diff --git a/suite/auto-sync/pyproject.toml b/suite/auto-sync/pyproject.toml index bc77298317..f1492e1881 100644 --- a/suite/auto-sync/pyproject.toml +++ b/suite/auto-sync/pyproject.toml @@ -7,7 +7,7 @@ name = "autosync" version = "0.1.0" dependencies = [ "termcolor >= 2.3.0", - "tree_sitter >= 0.21.3", + "tree_sitter == 0.22.3", "tree-sitter-cpp >=0.22.0", "black >= 24.3.0", "usort >= 1.0.8", diff --git a/suite/auto-sync/src/autosync/cpptranslator/Configurator.py b/suite/auto-sync/src/autosync/cpptranslator/Configurator.py index f50e9d6928..e8e81cba6d 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/Configurator.py +++ b/suite/auto-sync/src/autosync/cpptranslator/Configurator.py @@ -57,6 +57,12 @@ def get_general_config(self) -> dict: self.load_config() return self.config["General"] + def get_patch_config(self) -> dict: + if self.config: + return self.config["General"]["patching"] + self.load_config() + return self.config["General"]["patching"] + def load_config(self) -> None: if not Path.exists(self.config_path): fail_exit(f"Could not load arch config file at '{self.config_path}'") diff --git a/suite/auto-sync/src/autosync/cpptranslator/CppTranslator.py b/suite/auto-sync/src/autosync/cpptranslator/CppTranslator.py index 6e4992e7c2..38d28adac5 100755 --- a/suite/auto-sync/src/autosync/cpptranslator/CppTranslator.py +++ b/suite/auto-sync/src/autosync/cpptranslator/CppTranslator.py @@ -382,32 +382,16 @@ def patch_src(self, p_list: [(bytes, Node)]) -> None: def apply_patch(self, patch: Patch) -> bool: """Tests if the given patch should be applied for the current architecture or file.""" - has_apply_only = ( - len(patch.apply_only_to["files"]) > 0 - or len(patch.apply_only_to["archs"]) > 0 - ) - has_do_not_apply = ( - len(patch.do_not_apply["files"]) > 0 or len(patch.do_not_apply["archs"]) > 0 - ) - - if not (has_apply_only or has_do_not_apply): - # Lists empty. + apply_only_to = self.configurator.get_patch_config()["apply_patch_only_to"] + patch_name = patch.__class__.__name__ + if patch_name not in apply_only_to: + # No constraints return True - if has_apply_only: - if self.arch in patch.apply_only_to["archs"]: - return True - elif self.current_src_path_in.name in patch.apply_only_to["files"]: - return True - return False - elif has_do_not_apply: - if self.arch in patch.do_not_apply["archs"]: - return False - elif self.current_src_path_in.name in patch.do_not_apply["files"]: - return False + file_constraints = apply_only_to[patch_name] + if self.current_src_path_in.name in file_constraints["files"]: return True - log.fatal("Logical error.") - exit(1) + return False def translate(self) -> None: for self.current_src_path_in, self.current_src_path_out in zip( diff --git a/suite/auto-sync/src/autosync/cpptranslator/Tests/Patches/test_arch_config.json b/suite/auto-sync/src/autosync/cpptranslator/Tests/Patches/test_arch_config.json index 6fc12a6141..276da32d35 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/Tests/Patches/test_arch_config.json +++ b/suite/auto-sync/src/autosync/cpptranslator/Tests/Patches/test_arch_config.json @@ -5,7 +5,30 @@ "diff_color_saved": "yellow", "diff_color_edited": "light_magenta", "patch_editor": "vim", - "nodes_to_diff": [] + "nodes_to_diff": [], + "patching": { + "apply_patch_only_to": { + "AddCSDetail": { + "files": [ + "ARMInstPrinter.cpp", + "PPCInstPrinter.cpp", + "AArch64InstPrinter.cpp", + "LoongArchInstPrinter.cpp", + "MipsInstPrinter.cpp" + ] + }, + "InlineToStaticInline": { + "files": [ + "ARMAddressingModes.h" + ] + }, + "PrintRegImmShift": { + "files": [ + "ARMInstPrinter.cpp" + ] + } + } + } }, "ARCH": { "files_to_translate": [], diff --git a/suite/auto-sync/src/autosync/cpptranslator/Tests/test_unit.py b/suite/auto-sync/src/autosync/cpptranslator/Tests/test_unit.py new file mode 100644 index 0000000000..ee069211b4 --- /dev/null +++ b/suite/auto-sync/src/autosync/cpptranslator/Tests/test_unit.py @@ -0,0 +1,44 @@ +# SPDX-FileCopyrightText: 2024 Rot127 +# SPDX-License-Identifier: BSD-3 + +import unittest +from pathlib import Path + +from autosync.Helper import get_path +from autosync.cpptranslator import CppTranslator +from autosync.cpptranslator.Configurator import Configurator +from autosync.cpptranslator.patches.AddCSDetail import AddCSDetail +from autosync.cpptranslator.patches.InlineToStaticInline import InlineToStaticInline +from autosync.cpptranslator.patches.PrintRegImmShift import PrintRegImmShift +from autosync.cpptranslator.patches.Data import Data + + +class TestCppTranslator(unittest.TestCase): + @classmethod + def setUpClass(cls): + configurator = Configurator("ARCH", get_path("{PATCHES_TEST_CONFIG}")) + cls.translator = CppTranslator.Translator(configurator, False) + + def test_patching_constraints(self): + self.translator.current_src_path_in = Path("Random_file.cpp") + patch_add_cs_detail = AddCSDetail(0, "ARCH") + patch_inline_to_static_inline = InlineToStaticInline(0) + patch_print_reg_imm_shift = PrintRegImmShift(0) + patch_data = Data(0) + + self.assertFalse(self.translator.apply_patch(patch_add_cs_detail)) + self.assertFalse(self.translator.apply_patch(patch_inline_to_static_inline)) + self.assertFalse(self.translator.apply_patch(patch_print_reg_imm_shift)) + self.assertTrue(self.translator.apply_patch(patch_data)) + + self.translator.current_src_path_in = Path("ARMInstPrinter.cpp") + self.assertTrue(self.translator.apply_patch(patch_add_cs_detail)) + self.assertFalse(self.translator.apply_patch(patch_inline_to_static_inline)) + self.assertTrue(self.translator.apply_patch(patch_print_reg_imm_shift)) + self.assertTrue(self.translator.apply_patch(patch_data)) + + self.translator.current_src_path_in = Path("ARMAddressingModes.h") + self.assertFalse(self.translator.apply_patch(patch_add_cs_detail)) + self.assertTrue(self.translator.apply_patch(patch_inline_to_static_inline)) + self.assertFalse(self.translator.apply_patch(patch_print_reg_imm_shift)) + self.assertTrue(self.translator.apply_patch(patch_data)) diff --git a/suite/auto-sync/src/autosync/cpptranslator/arch_config.json b/suite/auto-sync/src/autosync/cpptranslator/arch_config.json index b16db63f92..3de3c4efe5 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/arch_config.json +++ b/suite/auto-sync/src/autosync/cpptranslator/arch_config.json @@ -5,6 +5,29 @@ "diff_color_saved": "yellow", "diff_color_edited": "light_magenta", "patch_editor": "vim", + "patching": { + "apply_patch_only_to": { + "AddCSDetail": { + "files": [ + "ARMInstPrinter.cpp", + "PPCInstPrinter.cpp", + "AArch64InstPrinter.cpp", + "LoongArchInstPrinter.cpp", + "MipsInstPrinter.cpp" + ] + }, + "InlineToStaticInline": { + "files": [ + "ARMAddressingModes.h" + ] + }, + "PrintRegImmShift": { + "files": [ + "ARMInstPrinter.cpp" + ] + } + } + }, "nodes_to_diff": [ { "node_type": "function_definition", diff --git a/suite/auto-sync/src/autosync/cpptranslator/patches/AddCSDetail.py b/suite/auto-sync/src/autosync/cpptranslator/patches/AddCSDetail.py index fcb5f912bf..84afa6952c 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/patches/AddCSDetail.py +++ b/suite/auto-sync/src/autosync/cpptranslator/patches/AddCSDetail.py @@ -31,6 +31,7 @@ class AddCSDetail(Patch): valid_param_lists = [ b"(MCInst*MI,unsignedOpNum,SStream*O)", # Default printOperand parameters. b"(MCInst*MI,unsignedOpNo,SStream*O)", # ARM - printComplexRotationOp / PPC default + b"(MCInst*MI,intopNum,SStream *O)", # Mips - printMemOperandEA and others b"(SStream*O,ARM_AM::ShiftOpcShOpc,unsignedShImm,boolgetUseMarkup())", # ARM - printRegImmShift b"(MCInst*MI,unsignedOpNo,SStream*O,constchar*Modifier)", # PPC - printPredicateOperand b"(MCInst*MI,uint64_tAddress,unsignedOpNo,SStream*O)", # PPC - printBranchOperand @@ -39,15 +40,6 @@ class AddCSDetail(Patch): def __init__(self, priority: int, arch: str): super().__init__(priority) self.arch = arch - self.apply_only_to = { - "files": [ - "ARMInstPrinter.cpp", - "PPCInstPrinter.cpp", - "AArch64InstPrinter.cpp", - "LoongArchInstPrinter.cpp", - ], - "archs": list(), - } def get_search_pattern(self) -> str: return ( @@ -88,11 +80,15 @@ def get_add_cs_detail( ) # Remove "print" from function id is_template = fcn_def.prev_sibling.type == "template_parameter_list" - op_num_var_name = ( - b"OpNum" - if b"OpNum" in params - else (b"OpNo" if b"OpNo" in params else b"-.-") - ) + if b"OpNum" in params: + op_num_var_name = b"OpNum" + elif b"OpNo" in params: + op_num_var_name = b"OpNo" + elif b"opNum" in params: + op_num_var_name = b"opNum" + else: + raise ValueError("OpNum parameter could not be identified.") + if not is_template and op_num_var_name in params: # Standard printOperand() parameters mcinst_var = get_MCInst_var_name(src, fcn_def) diff --git a/suite/auto-sync/src/autosync/cpptranslator/patches/InlineToStaticInline.py b/suite/auto-sync/src/autosync/cpptranslator/patches/InlineToStaticInline.py index 8d07e8e099..9c267811ac 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/patches/InlineToStaticInline.py +++ b/suite/auto-sync/src/autosync/cpptranslator/patches/InlineToStaticInline.py @@ -19,7 +19,6 @@ class InlineToStaticInline(Patch): def __init__(self, priority: int): super().__init__(priority) - self.apply_only_to = {"files": ["ARMAddressingModes.h"], "archs": list()} def get_search_pattern(self) -> str: return ( diff --git a/suite/auto-sync/src/autosync/cpptranslator/patches/Patch.py b/suite/auto-sync/src/autosync/cpptranslator/patches/Patch.py index 6f82a0b494..a08cb72ead 100644 --- a/suite/auto-sync/src/autosync/cpptranslator/patches/Patch.py +++ b/suite/auto-sync/src/autosync/cpptranslator/patches/Patch.py @@ -9,17 +9,6 @@ class Patch: priority: int = None - # List of filenames and architectures this patch applies to or not. - # Order of testing: - # 1. apply_only_to.archs - # 2. apply_only_to.files - # 3. do_not_apply.archs - # 4. do_not_apply.files - # Contains the _in_ filenames and architectures this patch should be applied to. Empty list means all. - apply_only_to = {"files": list(), "archs": list()} - # Contains the _in_ filenames and architectures this patch should NOT be applied to. - do_not_apply = {"files": list(), "archs": list()} - def __init__(self, priority: int = 0): self.priority = priority