Skip to content

MWCC Struct copy improvements #294

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

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

dbalatoni13
Copy link
Contributor

  • Adjusted the pattern to make it possible for addresses to not start at zero
  • Sometimes the last part uses different registers, this was adjusted in the pattern
  • Use a store statement instead of the void function we had till now. This helps with type resolution.

if dest.type.is_struct() or source.type.is_struct():
dest.type.unify(source.type)

# TODO are these four needed?
Copy link
Contributor Author

@dbalatoni13 dbalatoni13 Mar 29, 2025

Choose a reason for hiding this comment

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

Are these needed here? I copied them from another place (the next four lines)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's needed. Can you break that part out into a shared emit_write function maybe?

return line
else:
return fmt.with_comments(
line, [f"size 0x{fmt.format_hex(self.commented_size)}"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This causes an extra level of identation, do you have a suggestion maybe how we could get rid of that?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Eh, it's fine.

It looks like fmt.with_comments gives additional indentation though, we need to fix that somehow.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, wait, I must have been really tired when reading this, I thought you were referring to Python indentation...

I think the comments probably have to be carried all the way to if_statements somehow. Maybe Statement can grow a method for getting comments from a statement (defaulting to []), and SimpleStatement can check the output of that method?

@dbalatoni13
Copy link
Contributor Author

dbalatoni13 commented Mar 29, 2025

TODO: There's a bug that causes the copy to go onto ->unk0 instead of the argument. Another example is sp8[0] = lbl_8011E300; instead of sp8 = lbl_8011E300;.

@dbalatoni13
Copy link
Contributor Author

dbalatoni13 commented Mar 29, 2025

We do support addresses not starting at 0 now, but I wonder if that causes false positives on -O4.

@dbalatoni13 dbalatoni13 marked this pull request as draft March 29, 2025 23:25
@dbalatoni13
Copy link
Contributor Author

It also seems to make no difference whether we manually call dest.type.unify(source.type) in the code. It probably already happens somewhere else.

@@ -5,35 +5,35 @@ void test(void) {

}

void test_0(s32 arg0, s32 arg1) {
M2C_STRUCT_COPY(arg0, arg1, 8);
void test_0(? *arg0, ? *arg1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should probably get these to be typed as pointers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

?* is a pointer

if dest.type.is_struct() or source.type.is_struct():
dest.type.unify(source.type)

# TODO are these four needed?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's needed. Can you break that part out into a shared emit_write function maybe?

Comment on lines +1118 to +1120
size_expr = a.imm(2)
assert isinstance(size_expr, Literal)
size = size_expr.value
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
size_expr = a.imm(2)
assert isinstance(size_expr, Literal)
size = size_expr.value
size = a.imm_value(2)

@@ -2075,7 +2076,14 @@ def format(self, fmt: Formatter) -> str:
) or isinstance(dest, (ArrayAccess, LocalVar, SubroutineArg)):
# Known destination; fine to elide some casts.
source = elide_literal_casts(source)
return format_assignment(dest, source, fmt)
line = format_assignment(dest, source, fmt)
# Used in struct copy statements
Copy link
Collaborator

Choose a reason for hiding this comment

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

Drop this comment, it's mostly irrelevant it is used and it's confusing what this comment refers to exactly

return line
else:
return fmt.with_comments(
line, [f"size 0x{fmt.format_hex(self.commented_size)}"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Eh, it's fine.

It looks like fmt.with_comments gives additional indentation though, we need to fix that somehow.

@@ -5,35 +5,35 @@ void test(void) {

}

void test_0(s32 arg0, s32 arg1) {
M2C_STRUCT_COPY(arg0, arg1, 8);
void test_0(? *arg0, ? *arg1) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

?* is a pointer

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