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 the concept of "collection" to the compiler and remove the concept of "in-order AST nodes" #1819

Closed
6 of 7 tasks
emilyaherbert opened this issue Jun 2, 2022 · 3 comments
Assignees
Labels
big this task is hard and will take a while compiler: codegen Everything to do with IR->ASM, register allocation, etc. compiler: collection Everything to do with graph collection, type collection, and the collection context. compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen enhancement New feature or request language feature Core language features visible to end users Needs RFC Features that require an RFC before proceeding with an implementation P: high Should be looked at if there are no critical issues left
Milestone

Comments

@emilyaherbert
Copy link
Contributor

emilyaherbert commented Jun 2, 2022

The Problem

Currently, the Sway compiler relies heavily on the concept of the AST nodes being ordered “correctly” in order to perform successful compilation. Out of this comes the additional requirement that dependencies be ordered correctly in a Sway project. The burden of deciding the “correct AST node ordering” is not placed on the user, but on the compiler. Currently, the compiler performs a pass before type checking to determine the correct order in which to type check the AST nodes.

Mathematically, this places restrictions on the dependency structure of a Sway project. Currently, Sway projects must be directed acyclic graphs (a tree).

This creates fundamental issues:

  • Mutually recursive dependencies are not currently supported, and would not ever be able to be supported without extensive special-casing.
  • Mutually recursive AST nodes within the same file are not currently supported, and would not ever be able to be supported without extensive special-casing.
  • This inhibits the introduction of recursive functions, recursive trait methods, recursive struct and enum declarations, and more.
  • If a user forgets to import a dependency, it is not possible to suggest an import to them without extensive special-casing.

The Solution

The order of AST nodes should not be a factor of importance inside the compiler. I propose that we shift the way that the compiler thinks about AST node ordering, and eliminate the concept of “correct AST node ordering.” This will mean that the order of AST nodes during type-checking will no longer have any effect on the output of the compiler.

This will open up Sway to a new domain of applications, as it would allow Sway projects to be directed cyclic graphs. (two direct mutually recursive dependencies are considered a 1-hop cycle)

I propose these new high-level compiler steps:

  1. Parse all files in a Sway project.
  2. Perform a “collection pass” on the untyped parsed AST and gather information about all of the AST nodes, without type checking. This will generate a “collection environment” containing key information about the AST nodes.
  3. Perform type checking using the “collection environment,” allowing every AST node to have knowledge of the existence of every other AST node. This step will require us to introduce the concepts of a “declaration engine” that will operate similarly to the existing type engine. The declaration engine will work with the collection context to allow AST nodes to undergo type checking, without the need for dummy declarations, multiple passes, or substitutions. Additionally, the introduction of the declaration engine supports the introduction of other Sway features which are currently open on GitHub.
  4. Generate IR and do codegen

The Roadmap

The implementation steps are (roughly) as follows in order:

The original proposal is here: https://docs.google.com/document/d/1kwOk08-Ot7n2RCYCWNNtFCfQAxtlYN-DFHk2sxK139E/edit#

@emilyaherbert emilyaherbert added enhancement New feature or request big this task is hard and will take a while compiler: ir IRgen and sway-ir including optimization passes language feature Core language features visible to end users P: high Should be looked at if there are no critical issues left compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser labels Jun 2, 2022
@emilyaherbert emilyaherbert added compiler: collection Everything to do with graph collection, type collection, and the collection context. compiler: codegen Everything to do with IR->ASM, register allocation, etc. labels Jun 2, 2022
@adlerjohn adlerjohn moved this to Todo in Fuel Network Jun 2, 2022
@mohammadfawaz mohammadfawaz added the Needs RFC Features that require an RFC before proceeding with an implementation label Jun 27, 2022
@emilyaherbert emilyaherbert removed compiler: ir IRgen and sway-ir including optimization passes compiler: parser Everything to do with the parser labels Aug 26, 2022
This was referenced Sep 12, 2022
@emilyaherbert emilyaherbert removed their assignment Jun 1, 2023
@IGI-111 IGI-111 added this to the October 2023 milestone Jul 6, 2023
@IGI-111 IGI-111 assigned tritao and unassigned anton-trunov Jul 20, 2023
@kayagokalp
Copy link
Member

@tritao I was doing a little clean-up and realized this issue is marked as done in compiler board but still open. Is this issue still relevant?

@tritao
Copy link
Contributor

tritao commented Oct 27, 2024

The semantic analysis part is done, I didn't close this due to not being sure about #1823.

Perhaps @ironcev or @vaivaswatha can chime in if that's something that's still needed or if we can close it as well.

@ironcev
Copy link
Member

ironcev commented Oct 28, 2024

#1823 discusses the very first IR generation that didn't have function calls but was inlining everything and proposes what we do have now, in the IR and in ASM generation, calls and jumps respectively. So I've closed it.

@tritao tritao closed this as completed Oct 28, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Fuel Network Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big this task is hard and will take a while compiler: codegen Everything to do with IR->ASM, register allocation, etc. compiler: collection Everything to do with graph collection, type collection, and the collection context. compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen enhancement New feature or request language feature Core language features visible to end users Needs RFC Features that require an RFC before proceeding with an implementation P: high Should be looked at if there are no critical issues left
Projects
Status: Done
Development

No branches or pull requests

7 participants