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

fix float issues and add decompctx.py tool #15

Merged
merged 1 commit into from
Sep 12, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ orig/*/*
tools/mwcc_compiler/*
!tools/mwcc_compiler/.gitkeep
/*.txt
ctx.c
4 changes: 2 additions & 2 deletions include/MSL_C/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ inline double sqrt_step(double tmpd, float mag) {
}

extern inline float sqrtf(float x) {
static const double _half = .5;
static const double _three = 3.0;
const double _half = .5;
const double _three = 3.0;
volatile float y;
if (x > 0.0f) {
double guess = __frsqrte((double)x); // returns an approximation to
Expand Down
77 changes: 77 additions & 0 deletions tools/decompctx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3

import argparse
import os
import re

script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
src_dir = os.path.join(root_dir, "src")
include_dir = os.path.join(root_dir, "include")

include_pattern = re.compile(r'^#include\s*[<"](.+?)[>"]$')
guard_pattern = re.compile(r'^#ifndef\s+(.*)$')

defines = set()

def import_h_file(in_file: str, r_path: str) -> str:
rel_path = os.path.join(root_dir, r_path, in_file)
inc_path = os.path.join(include_dir, in_file)
if os.path.exists(rel_path):
return import_c_file(rel_path)
elif os.path.exists(inc_path):
return import_c_file(inc_path)
else:
print("Failed to locate", in_file)
exit(1)

def import_c_file(in_file) -> str:
in_file = os.path.relpath(in_file, root_dir)
out_text = ''

try:
with open(in_file, encoding="shift-jis") as file:
out_text += process_file(in_file, list(file))
except Exception:
with open(in_file) as file:
out_text += process_file(in_file, list(file))
return out_text

def process_file(in_file: str, lines) -> str:
out_text = ''
for idx, line in enumerate(lines):
guard_match = guard_pattern.match(line.strip())
if idx == 0:
if guard_match:
if guard_match[1] in defines:
break
defines.add(guard_match[1])
print("Processing file", in_file)
include_match = include_pattern.match(line.strip())
if include_match and not include_match[1].endswith(".s"):
out_text += f"/* \"{in_file}\" line {idx} \"{include_match[1]}\" */\n"
out_text += import_h_file(include_match[1], os.path.dirname(in_file))
out_text += f"/* end \"{include_match[1]}\" */\n"
else:
out_text += line

return out_text

def main():
parser = argparse.ArgumentParser(
description="""Create a context file which can be used for decomp.me"""
)
parser.add_argument(
"c_file",
help="""File from which to create context""",
)
args = parser.parse_args()

output = import_c_file(args.c_file)

with open(os.path.join(root_dir, "ctx.c"), "w", encoding="utf-8") as f:
f.write(output)


if __name__ == "__main__":
main()