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

Added compatibility for decomp-toolkit macros #275

Merged
merged 2 commits into from
May 29, 2024

Conversation

dbalatoni13
Copy link
Contributor

@dbalatoni13 dbalatoni13 commented May 28, 2024

encounter/decomp-toolkit outputs assembly files that are currently not fully compatible with m2c.
One problem is that labels have the format

.obj name, local
    .float 0
.endobj name

This makes m2c not detect any data, bss, rodata or jump table labels.
Another problem is that jump tables have the prefix "jumptable_".
Ultimately, strings use the type ".string".

m2c/asm_file.py Outdated
".asciz",
".ascii",
".asciiz",
"string",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work -- this section is guarded by directive.startswith(".")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, it should actually be ".string"

m2c/asm_file.py Outdated
@@ -339,7 +339,9 @@ def re_comment_replacer(match: Match[str]) -> str:
re_whitespace_or_string = re.compile(r'\s+|"(?:\\.|[^\\"])*"')
re_local_glabel = re.compile("L(_.*_)?[0-9A-F]{8}")
re_local_label = re.compile("loc_|locret_|def_|lbl_|LAB_|jump_")
re_label = re.compile(r'(?:([a-zA-Z0-9_.$]+)|"([a-zA-Z0-9_.$<>@,-]+)"):')
re_label = re.compile(
r'(?:(?:\.obj )?([a-zA-Z0-9_.$]+)|"([a-zA-Z0-9_.$<>@,-]+)")[:,]'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of complicating this regex, can you add a case near where jlabel/glabel/etc are handled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will give it a try.

@simonlindholm
Copy link
Collaborator

Thanks, compatibility with more formats is always welcome. (Sorry for not responding in time to #272, I've been a bit strapped for time.)

@dbalatoni13
Copy link
Contributor Author

No problem!

@dbalatoni13
Copy link
Contributor Author

Does it look alright? I also made sure to set z to True for .string.

@dbalatoni13
Copy link
Contributor Author

dbalatoni13 commented May 28, 2024

Sorry, my previous edit deleted the jump table change, I'm not really experienced with git yet. I added it back now. Why does black change the formatting for something that was already in the repo?

m2c/asm_file.py Outdated
@@ -548,6 +554,14 @@ def process_label(label: str, *, kind: LabelKind) -> None:
data = parse_incbin(args, options, warnings)
if data is not None:
asm_file.new_data_bytes(data)
elif directive == ".obj": # decomp-toolkit label format
parts = line.split()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make use of args instead, which is the arguments split by comma? we don't want to make unnecessary assumptions about whitespace here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

m2c/asm_file.py Outdated
parts = line.split()
if len(parts) >= 3:
try:
kind = LabelKind[parts[2].upper()]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LabelKind[...] exposes internal details in an ugly way and isn't searchable. Can you use some manual if-else switch instead?

Copy link
Contributor Author

@dbalatoni13 dbalatoni13 May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it fine if I set it to global by default and change it to local if the string equals local? Or rather not do anything if the value is not one of those?
I did some testing and my variables get marked as static not depending on the LabelKind at all, is that normal?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's fine. A global label here means one that starts a function; for data it doesn't make a difference whether the label is global or not. And yeah I think m2c will happily mark things as static regardless of this, it's basing that decision on the context file instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

@@ -862,7 +860,7 @@ def find_block_by_label(label: str) -> Optional[Block]:
and isinstance(arg.argument, AsmGlobalSymbol)
and any(
arg.argument.symbol_name.startswith(prefix)
for prefix in ("jtbl", "jpt_", "lbl_")
for prefix in ("jtbl", "jpt_", "lbl_", "_jumptable")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just double checking: _jumptable vs jumptable_?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.

m2c/asm_file.py Outdated
@@ -548,6 +554,12 @@ def process_label(label: str, *, kind: LabelKind) -> None:
data = parse_incbin(args, options, warnings)
if data is not None:
asm_file.new_data_bytes(data)
elif directive == ".obj": # decomp-toolkit label format
if len(args) == 2:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert len(args) == 2 instead to have some minimum of error handling. Or raise DecompFailure("...") in an else branch but that feels like effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will use assert

m2c/asm_file.py Outdated
Comment on lines 559 to 561
kind = LabelKind.GLOBAL
if args[1] == "local":
kind = LabelKind.LOCAL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
kind = LabelKind.GLOBAL
if args[1] == "local":
kind = LabelKind.LOCAL
kind = LabelKind.LOCAL if args[1] == "local" else LabelKind.GLOBAL

if you want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, black adds brackets btw

m2c/asm_file.py Outdated
@@ -548,6 +554,12 @@ def process_label(label: str, *, kind: LabelKind) -> None:
data = parse_incbin(args, options, warnings)
if data is not None:
asm_file.new_data_bytes(data)
elif directive == ".obj": # decomp-toolkit label format
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, maybe move this up next to the handling for .rel which is also related to decomp-toolkit?

@dbalatoni13
Copy link
Contributor Author

I ran black and it was strange that it changed the formatting in other sections of this file and also multiple other files. I didn't commit those other changes, what do you think the issue probably is?

@simonlindholm
Copy link
Collaborator

Test failure due to too old black version? Because I don't get reformatting in flow_graph.py locally.

@simonlindholm
Copy link
Collaborator

23.12.1 is the version used in CI btw (mentioned in pyproject.toml), and 24.4.2 seems to be behave the same for me. You can probably just python3 -m pip install --upgrade black

@simonlindholm
Copy link
Collaborator

Actually, nvm, 23.12.1 and 24.4.2 do not behave the same, I just had parallel install of black somehow. (depot_tools??? this is a new way of screwing up python installs that I heretofore was not aware of.) I'll push an update to master to make things simple.

@simonlindholm
Copy link
Collaborator

so just rebase or merge with master and you should be good

@dbalatoni13
Copy link
Contributor Author

Oh, okay. I did a rebase.

@simonlindholm simonlindholm merged commit 81b1748 into matt-kempster:master May 29, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants