Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompme #237

Merged
merged 4 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def patch_compilers():
"-inline auto",
"-w notinlined -W noimplicitconv -w nounwanted",
"-nostdinc",
"-msgstyle gcc -lang=c99 -DREVOKART",
"-msgstyle gcc -DREVOKART",
"-func_align 4",
#"-sym dwarf-2",
]
Expand Down
7 changes: 5 additions & 2 deletions mkwutil/tools/create_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
"-func_align 4",
"-stderr",
"-E",
"-ppopt space",
]
CWCC_OPT = " ".join(CW_ARGS)


def get_ctx(match: str, out: Path):
def get_ctx(match: str, out: Path, verbose=True):
source = next(x for x in chain(SOURCES_DOL, SOURCES_REL) if match in x.src)
source_header = Path(source.src).with_suffix(".hpp")
print(f"Generating context for {source_header} at {out}")
if verbose:
print(f"Generating context for {source_header} at {out}")
command = f"{CWCC_PATHS[source.cc]} {CWCC_OPT} {source_header} -o {out}"
lines, retcode = run_windows_cmd(command)
return source


if __name__ == "__main__":
Expand Down
76 changes: 76 additions & 0 deletions mkwutil/tools/decompme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
Creates a decomp.me scratch for a function
Inspired by https://github.com/SeekyCt/spm-decomp/blob/master/decompme.py
Modified for mkw ops
"""

from argparse import ArgumentParser
from pathlib import Path
import os

import requests

from mkwutil.tools.create_ctx import get_ctx
from mkwutil.project import read_symbol_map
from mkwutil.ppcdis_adapter.ppcdis_disas import get_dol_disaser, get_rel_disaser
from configure import CWCC_OPT
from ast import literal_eval

# Get arguments
parser = ArgumentParser()
parser.add_argument("sym", type=str, help="Symbol name or address")
parser.add_argument("-m", "--match", type=str, help="String contained in desired context header file")
parser.add_argument("--host", default="https://decomp.me")
args = parser.parse_args()

# Find address and diff_label
symbols = read_symbol_map(Path("pack") / "symbols.yml")
symbols.derive_sizes(0x8100_0000)
try:
addr = literal_eval(args.sym)
sym = symbols.get(addr)
except:
sym = symbols.get_by_name(args.sym)
if sym is not None:
addr = sym.addr
diff_label = sym.name
else:
print(f"Could not find symbol with name/address {args.sym}")
exit(-1)

# Get flags for binary
if addr < 0x8050_0000:
preset = "Mario Kart Wii (DOL)"
disaser = get_dol_disaser()
else:
preset = "Mario Kart Wii (REL)"
disaser = get_rel_disaser()

# Disassemble function
fn_start_vma = addr
fn_end_vma = fn_start_vma + sym.size
asm, _ = disaser.function_to_text_with_referenced(fn_start_vma, inline=False, extra=True,
em-eight marked this conversation as resolved.
Show resolved Hide resolved
hashable=False, declare_mangled=False)

# Get context
tmpfile = Path("ctx.hpp")
source = get_ctx(args.match, tmpfile, verbose=False)
flags = CWCC_OPT + ' ' + source.opts
with open(tmpfile, 'r') as file:
ctx = file.read()
os.unlink(tmpfile)

# Send request
req = {
"target_asm" : asm,
"context" : ctx,
"platform" : "gc_wii",
# TODO: Different compiler for DOL
"compiler" : "mwcc_42_127",
"compiler_flags" : flags,
"preset" : preset,
"diff_label" : diff_label
}
r = requests.post(args.host + "/api/scratch", json=req)
assert r.status_code == 201, f"Bad status code {r.status_code}"
print(args.host + r.json()["html_url"])
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pytest==7.1.1
python-dateutil==2.8.2
python-Levenshtein==0.12.2
pytz==2022.1
requests==2.28.1
six==1.16.0
tabledata==1.2.0
tcolorpy==0.1.2
Expand Down
12 changes: 6 additions & 6 deletions sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
from itertools import chain


HOSTSYS_OPTS = '-ipa file -rostr -sdata 0 -sdata2 0'
RVL_OPTS = '-ipa file'
MSL_LIBC_OPTS = '-ipa file'
NW4R_OPTS = '-ipa file -inline auto -O4,p -pragma \"legacy_struct_alignment on\"'
HOSTSYS_OPTS = ' -ipa file -rostr -sdata 0 -sdata2 0'
RVL_OPTS = ' -lang=c99 -ipa file'
MSL_LIBC_OPTS = ' -lang=c99 -ipa file'
NW4R_OPTS = ' -lang=c99 -ipa file -inline auto -O4,p -pragma \"legacy_struct_alignment on\"'
SPY_OPTS = RVL_OPTS + " -w nounusedexpr -w nounusedarg"
RFL_OPTS = RVL_OPTS + " -O4,p"
EGG_OPTS = ' -use_lmw_stmw=on -ipa function -rostr'
REL_OPTS = HOSTSYS_OPTS + " -use_lmw_stmw=on -pragma \"legacy_struct_alignment on\" "
EGG_OPTS = ' -lang=c99 -use_lmw_stmw=on -ipa function -rostr'
REL_OPTS = HOSTSYS_OPTS + " -lang=c++ -use_lmw_stmw=on -pragma \"legacy_struct_alignment on\" "


@dataclass
Expand Down