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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ The `-march=` value to pass to GNU as, defaults to `allegrex`
### `--as-mabi`
The `-mabi=` value to pass to GNU as, defaults to `32`

### `--as-flags`
Additional flags to pass to GNU as, defaults to `-G0`.

### `--use-wibo`
Whether or not to prefix the call to the MWCC executable with [wibo](https://github.com/decompals/wibo), defaults to **false**.

Expand All @@ -48,7 +51,8 @@ Optional encoding that the input c file should be converted to, before being pas
### `--src-dir`
Optional path to use when passing data over stdin to interpret relative path include statements.

All additional arguments will be passed to the MWCC executable.

**NOTE:** Any additional arguments will be passed through to the MWCC executable.


## Quirks
Expand Down
48 changes: 32 additions & 16 deletions mwccgap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
from mwccgap.mwccgap import process_c_file


class CustomTildeFormatter(argparse.HelpFormatter):
def format_help(self):
text = super().format_help()
return text.replace("~~", "--")


def main() -> None:
parser = argparse.ArgumentParser()
"""
Hack: We replace `--` with `~~` before parsing so argparse ignores user-supplied
flags meant for the assembler. The prefixes are restored afterward.
"""
parser = argparse.ArgumentParser(
prefix_chars="~", formatter_class=CustomTildeFormatter
)

read_from_file = sys.stdin.isatty()

if not read_from_file:
in_lines = sys.stdin.readlines()
if len(in_lines) == 0:
Expand All @@ -22,20 +33,26 @@ def main() -> None:
parser.add_argument("c_file", type=Path)

parser.add_argument("o_file", type=Path)
parser.add_argument("--mwcc-path", type=Path, default=Path("mwccpsp.exe"))
parser.add_argument("--as-path", type=Path, default=Path("mipsel-linux-gnu-as"))
parser.add_argument("--as-march", type=str, default="allegrex")
parser.add_argument("--as-mabi", type=str, default="32")
parser.add_argument("--use-wibo", action="store_true")
parser.add_argument("--wibo-path", type=Path, default=Path("wibo"))
parser.add_argument("--asm-dir-prefix", type=Path)
parser.add_argument("--macro-inc-path", type=Path)
parser.add_argument("--target-encoding", type=str)
parser.add_argument("--src-dir", type=Path)

args, c_flags = parser.parse_known_args()
default_as_flags = ["-G0"] # TODO: base this on -sdatathreshold value from c_flags

as_flags = ["-G0"] # TODO: base this on -sdatathreshold value from c_flags
def add_argument(arg, **kwargs):
parser.add_argument(arg.replace("--", "~~"), **kwargs)

add_argument("--mwcc-path", type=Path, default=Path("mwccpsp.exe"))
add_argument("--as-path", type=Path, default=Path("mipsel-linux-gnu-as"))
add_argument("--as-march", type=str, default="allegrex")
add_argument("--as-mabi", type=str, default="32")
add_argument("--as-flags", nargs="*", default=default_as_flags)
add_argument("--use-wibo", action="store_true")
add_argument("--wibo-path", type=Path, default=Path("wibo"))
add_argument("--asm-dir-prefix", type=Path)
add_argument("--macro-inc-path", type=Path)
add_argument("--target-encoding", type=str)
add_argument("--src-dir", type=Path)

argv = [arg.replace("--", "~~") for arg in sys.argv[1:]]
args, c_flags = parser.parse_known_args(argv)

try:
with tempfile.NamedTemporaryFile(suffix=".c", dir=args.src_dir) as temp_c_file:
Expand All @@ -44,7 +61,6 @@ def main() -> None:
if not read_from_file:
temp_c_file.writelines([x.encode("utf") for x in in_lines])
temp_c_file.flush()

process_c_file(
c_file,
args.o_file,
Expand All @@ -53,9 +69,9 @@ def main() -> None:
as_path=args.as_path,
as_march=args.as_march,
as_mabi=args.as_mabi,
as_flags=args.as_flags,
use_wibo=args.use_wibo,
wibo_path=args.wibo_path,
as_flags=as_flags,
asm_dir_prefix=args.asm_dir_prefix,
macro_inc_path=args.macro_inc_path,
c_file_encoding=args.target_encoding,
Expand Down
5 changes: 4 additions & 1 deletion mwccgap/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ def preprocess_s_file(
if line.startswith("/* Handwritten function"):
# ignore handwritten comment
continue
if line.startswith("/*") and re.sub(BLOCK_COMMENT_REGEX, "", line).strip() == "":
if (
line.startswith("/*")
and re.sub(BLOCK_COMMENT_REGEX, "", line).strip() == ""
):
# ignore multi-line comments
continue
if line.startswith("#"):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_dollar_symbol(self):
self.assertTrue(c_lines[0].startswith("static "))

def test_comments(self):
asm_contents ="""
asm_contents = """
/* Generated by spimdisasm 1.39.3 */

/* Handwritten function */
Expand All @@ -192,6 +192,7 @@ def test_comments(self):
self.assertEqual(expected_nops + 2, len(c_lines))
self.assertEqual(0, len(rodata_entries))


class TestPreprocessSFileExceptions(unittest.TestCase):
def test_rodata_unknown_directive(self):
asm_contents = """
Expand Down