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

Add map of imported procedures to AST structs #986

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

jjcnn
Copy link
Contributor

@jjcnn jjcnn commented Jun 30, 2023

Describe your changes

A map from procedure IDs of imported procedures to the procedure's name and module path has been added to ProgramAst and ModuleAst.

This map is needed for the MASM pretty-printer, because the instructions that represent calls to imported procedures only contain the procedure's ID and not its name. The procedure's ID is a hash, and it is therefore impossible to print the assembly for the call instruction without this map.

The map is populated during parsing as part of the ParserContext. Since the parser does not have access to the code of imported modules, only the procedures that are actually called are represented in the map. This also explains why the map is named used_imported_procs.

Like the imports map, this new map can optionally be serialized when the AST is serialized. Serialization and deserialization tests have been added.

The only user-facing change is the new limitation of max 2^16-1 calls to imported procedures in each program and module. We have not added documentation for similar restrictions in the past, so I haven't added anything this time either.

Checklist before requesting a review

  • Repo forked and branch created from next according to naming convention.
  • Commit messages and codestyle follow conventions.
  • Relevant issues are linked in the PR description.
  • Tests added for new functionality.
  • Documentation/comments updated according to changes.

@jjcnn jjcnn requested review from bobbinth and tohrnii June 30, 2023 14:14
Copy link
Contributor

@tohrnii tohrnii left a comment

Choose a reason for hiding this comment

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

Thank you. Looks good to me.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a small comment inline, but I also do wonder about one thing: is there a use-case when we'd want imports but not used imported procs and vice-versa? Or do they always go together?

My thinking was that they always go together and we can view them kind of like extra information about imports used within a module. So, maybe a better approach is to create a dedicate struct for this? This struct could look something like this:

pub struct ModuleImports {
    imported_modules: BTreeMap<String, LibraryPath>,
    used_procs: BTreeMap<ProcedureId, (ProcedureName, String)>,
}

This might be a good way to encapsulate this info, and we'd be able to add various convenience methods to this as well. For example:

impl ModuleImports {
    pub fn get_module_path(&self, module_name: &str) -> Option<LibraryPath> {
        todo!()
    }

    pub fn add_used_procedure(&mut self, proc_name: ProcedureName, module_name: &str) {
        todo!()
    }
 
    pub fn get_unused_imports(&self) -> &[LibraryPath] {
        todo!()
    }
}

And this struct could also be serialized (or not) in its entirety.

@jjcnn
Copy link
Contributor Author

jjcnn commented Jul 3, 2023

is there a use-case when we'd want imports but not used imported procs and vice-versa? Or do they always go together?

I'm very unsure about the use cases beyond pretty-printing. For pretty-printing we need both, and I can't imagine you would ever need the procedure names map without also needing the import paths, so there's definitely a dependency there.

I'll stick it all into a dedicated struct for now. We can always separate them out later if we need to.

@jjcnn jjcnn force-pushed the jjcnn-ast-imported-procedure-map branch 2 times, most recently from 0c46ab0 to d7d790d Compare July 4, 2023 18:30
@jjcnn
Copy link
Contributor Author

jjcnn commented Jul 5, 2023

Review comments addressed. The resulting refactoring is quite extensive, but it probably makes better sense this way.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left some inline comments. The biggest thing is that I think we could move more logic into the new ModuleImports struct - specifically parsing and validation logic. If we do the latter, we can also insure that ModuleImports is always in a valid state.

@jjcnn jjcnn force-pushed the jjcnn-ast-imported-procedure-map branch 2 times, most recently from 7f24a71 to bdf8a9e Compare July 7, 2023 12:02
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a couple more small comments. Also, take a look at my last commit to see if I missed anything there.

@jjcnn jjcnn force-pushed the jjcnn-ast-imported-procedure-map branch from e3966da to 6869f8f Compare July 10, 2023 11:05
@jjcnn
Copy link
Contributor Author

jjcnn commented Jul 10, 2023

Review comments fixed, and branch rebased to next.

@jjcnn jjcnn force-pushed the jjcnn-ast-imported-procedure-map branch from 6869f8f to 2dd0e35 Compare July 10, 2023 11:15
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a couple of doc-related comments inline. Once these are addressed, we can merge.

@jjcnn jjcnn force-pushed the jjcnn-ast-imported-procedure-map branch from 2dd0e35 to 785da42 Compare July 11, 2023 07:49
@jjcnn jjcnn merged commit 587289f into next Jul 11, 2023
@jjcnn jjcnn deleted the jjcnn-ast-imported-procedure-map branch July 11, 2023 08:02
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.

3 participants