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

Allow register indices in addition to ABI names #52

Merged
merged 4 commits into from
Nov 28, 2023

Conversation

superenginegit
Copy link
Contributor

This PR is for issue #51.

I have added a function to parse the arguments of an instruction, that is used to canonicalize the register names. This function could be expanded to do other things with the arguments in the future, if needed.

The canonicalization is done using a dict, that is used replace register-index-names with the according ABI-name.

Additionally I have added a file-check, that is just like the "hello world"-check but uses register-indices instead of ABI-names.

@superenginegit
Copy link
Contributor Author

@AntonLydike I think you didn't see my PR. I'd appreciate if you could review it.

Comment on lines 125 to 135
def canonicalize_register_names(args: Tuple[str]) -> Iterable[str]:
"""
Canonicalizes register names.
:param args: A tuple of an instructions arguments
:return: An iterator over the arguments of an instruction, but with canonicalized register names
"""
for arg in args:
if arg in REG_NAME_CANONICALIZER.keys():
yield REG_NAME_CANONICALIZER[arg]
else:
yield arg
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't really speak to how this would best fit with the overall architecture of the framework, but I'd recommend the following changes for this function specifically:

Suggested change
def canonicalize_register_names(args: Tuple[str]) -> Iterable[str]:
"""
Canonicalizes register names.
:param args: A tuple of an instructions arguments
:return: An iterator over the arguments of an instruction, but with canonicalized register names
"""
for arg in args:
if arg in REG_NAME_CANONICALIZER.keys():
yield REG_NAME_CANONICALIZER[arg]
else:
yield arg
def register_name_for_indexed(indexed_name: str) -> str:
"""
Canonicalizes register name.
:param args: A tuple of an instructions arguments.
:return: An iterator over the arguments of an instruction, but with canonicalized register names.
"""
return REG_NAME_CANONICALIZER.get(arg, arg)

The function name describes a bit better what it does, I would have expected the canonical name to be the indexed name. The function operates one element at a time, the client can decide whether to get just one name or a tuple or a list at the callsite (tuple(register_name_for_indexed(arg) for arg in args))). There's a built-in method for the pattern of getting a value with a default if it's not in the dictionary.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh and I think some examples in the docstring would be good

Copy link
Owner

Choose a reason for hiding this comment

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

Almost @superlopuh , I think you are missing that args is an array here.

But still, I'd simplify the if .. in ... and item getting into a single .get(arg, default) call.

Copy link
Owner

@AntonLydike AntonLydike left a comment

Choose a reason for hiding this comment

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

This looks pretty nice already, just some minor things left. Sorry, I seem to have missed finishing the last review I started on this.

riscemu/regs.py Show resolved Hide resolved
Comment on lines 125 to 135
def canonicalize_register_names(args: Tuple[str]) -> Iterable[str]:
"""
Canonicalizes register names.
:param args: A tuple of an instructions arguments
:return: An iterator over the arguments of an instruction, but with canonicalized register names
"""
for arg in args:
if arg in REG_NAME_CANONICALIZER.keys():
yield REG_NAME_CANONICALIZER[arg]
else:
yield arg
Copy link
Owner

Choose a reason for hiding this comment

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

Almost @superlopuh , I think you are missing that args is an array here.

But still, I'd simplify the if .. in ... and item getting into a single .get(arg, default) call.

Copy link
Owner

@AntonLydike AntonLydike left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

@AntonLydike AntonLydike merged commit 4184982 into AntonLydike:master Nov 28, 2023
@superenginegit superenginegit deleted the allow_register_indices branch November 28, 2023 07:17
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.

None yet

3 participants