-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
decomp.py
as an abstraction over m2c (#1090)
usage: decomp.py [-h] [--no-context] [--no-copy] [--no-print] [--colorize] function ... Decomp a function using m2c positional arguments: function a function to be processed m2c_args additional arguments to be passed to m2c options: -h, --help show this help message and exit --no-context do not generate ctx.c --no-copy do not copy the output to the clipboard --no-print do not print the output --colorize colorize the output (requires pygments)
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,13 @@ orig/*/sys/* | |
/*.txt | ||
|
||
# Python | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# .DS_Store | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
from sys import stderr | ||
from typing import Optional, cast | ||
|
||
from elftools.elf.elffile import ELFFile | ||
from elftools.elf.sections import SymbolTableSection | ||
|
||
root = Path(__file__).parent.parent | ||
dtk_root = root / "build/GALE01" | ||
obj_root = dtk_root / "obj" | ||
asm_root = dtk_root / "asm" | ||
m2c_script = root / "tools/m2c/m2c.py" | ||
ctx_file = root / "build/ctx.c" | ||
m2ctx_script = root / "tools/m2ctx/m2ctx.py" | ||
|
||
|
||
def has_function(obj_path: Path, function_name: str) -> bool: | ||
with open(obj_path, "rb") as f: | ||
elf_file = ELFFile(f) | ||
symbol_table = elf_file.get_section_by_name(".symtab") | ||
|
||
if isinstance(symbol_table, SymbolTableSection): | ||
for symbol in symbol_table.iter_symbols(): | ||
if ( | ||
symbol["st_info"]["type"] == "STT_FUNC" | ||
and symbol.name == function_name | ||
): | ||
return True | ||
return False | ||
|
||
|
||
def find_obj(root: Path, function_name: str) -> Optional[Path]: | ||
for p in root.rglob("*.o"): | ||
if has_function(p, function_name): | ||
return p.relative_to(root) | ||
return None | ||
|
||
|
||
def resolve_path(p: Path) -> str: | ||
return str(p.resolve()) | ||
|
||
|
||
def run_cmd(cmd: list[str]) -> str: | ||
result = subprocess.run(cmd, capture_output=True) | ||
if result.returncode != 0: | ||
print(" ".join(cmd)) | ||
print(result.stderr.decode(), file=sys.stderr) | ||
sys.exit(1) | ||
else: | ||
return result.stdout.decode() | ||
|
||
|
||
def gen_ctx(): | ||
run_cmd( | ||
[ | ||
"python", | ||
resolve_path(m2ctx_script), | ||
"--quiet", | ||
"--preprocessor", | ||
] | ||
) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Decomp a function using m2c") | ||
parser.add_argument( | ||
"function", | ||
type=str, | ||
help="a function to be processed", | ||
) | ||
parser.add_argument( | ||
dest="m2c_args", | ||
nargs=argparse.REMAINDER, | ||
help="additional arguments to be passed to m2c", | ||
) | ||
parser.add_argument( | ||
"--no-context", | ||
action="store_false", | ||
dest="ctx", | ||
help=f"do not generate {ctx_file.name}", | ||
) | ||
parser.add_argument( | ||
"--no-copy", | ||
action="store_false", | ||
dest="copy", | ||
help=f"do not copy the output to the clipboard", | ||
) | ||
parser.add_argument( | ||
"--no-print", | ||
action="store_false", | ||
dest="print", | ||
help=f"do not print the output", | ||
) | ||
parser.add_argument( | ||
"--colorize", | ||
action="store_true", | ||
dest="color", | ||
help=f"colorize the output (requires pygments)", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if (obj_file := find_obj(obj_root, args.function)) is not None: | ||
asm_file = asm_root / cast(Path, obj_file).with_suffix(".s") | ||
|
||
m2c_cmd: list[str] = [ | ||
"python", | ||
resolve_path(m2c_script), | ||
*args.m2c_args, | ||
"--target", | ||
"ppc-mwcc-c", | ||
"--context", | ||
resolve_path(ctx_file), | ||
"--function", | ||
args.function, | ||
resolve_path(asm_file), | ||
] | ||
|
||
if args.ctx: | ||
gen_ctx() | ||
|
||
output = run_cmd(m2c_cmd) | ||
if args.copy: | ||
import pyperclip | ||
|
||
pyperclip.copy(output) | ||
if args.print: | ||
if args.color: | ||
from pygments import highlight | ||
from pygments.formatters import TerminalFormatter | ||
from pygments.lexers import CLexer | ||
|
||
output = highlight(output, CLexer(), TerminalFormatter()) | ||
print(output, file=sys.stdout) | ||
else: | ||
print(f"Could not find {args.function}", file=stderr) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters