Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion suite/auto-sync/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions suite/auto-sync/src/autosync/cpptranslator/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'")
Expand Down
30 changes: 7 additions & 23 deletions suite/auto-sync/src/autosync/cpptranslator/CppTranslator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
44 changes: 44 additions & 0 deletions suite/auto-sync/src/autosync/cpptranslator/Tests/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2024 Rot127 <unisono@quyllur.org>
# 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))
23 changes: 23 additions & 0 deletions suite/auto-sync/src/autosync/cpptranslator/arch_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 10 additions & 14 deletions suite/auto-sync/src/autosync/cpptranslator/patches/AddCSDetail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
11 changes: 0 additions & 11 deletions suite/auto-sync/src/autosync/cpptranslator/patches/Patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down