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

Compiler refactor: replace Control Flow Graphs with builders #686

Merged
merged 60 commits into from
Jan 26, 2025

Conversation

Akuli
Copy link
Owner

@Akuli Akuli commented Jan 24, 2025

A builder defines how to do somewhat elementary things, e.g. a function/method call, conditional branch, or an equals comparison. Then a new file ast_to_builder.jou recurses over the AST and calls methods of the builder accordingly.

For now, there is only one builder, which builds LLVM IR. The idea is to later build a special, intentionally very limited "Jou IR" to solve #566.

Also, I really hated how the old CFG's worked. They were very similar to LLVM IR, but much shittier.

TODO:

  • get CI green
  • document this
  • resolve # TODO: rename to AstToBuilder
  • resolve # TODO: add error message for this, or dynamic size?
  • resolve # TODO: https://github.com/Akuli/jou/issues/683
  • resolve # TODO: tell llvm, if we know a function is noreturn ?
  • resolve # TODO: useful, but doesn't belong here
  • resolve # TODO: Everything named legacy_llvm14_cast can be removed once we drop LLVM 14 support
  • resolve # TODO: which casts are necessary on LLVM 14 and which are not?
  • resolve # TODO: method to find field
  • resolve # TODO: do we need is_main_file?
  • resolve # TODO: delete id
  • resolve # TODO: delete double pointer

@Akuli Akuli added the big A lot of work to implement label Jan 24, 2025
@Akuli
Copy link
Owner Author

Akuli commented Jan 24, 2025

Not gonna resolve all TODOs. This PR already deletes more TODOs than it adds.

@Akuli
Copy link
Owner Author

Akuli commented Jan 24, 2025

Don't know why the CI is failing now, and I cannot reproduce it locally. Maybe I'll fix finish this PR some other day :)

@Akuli
Copy link
Owner Author

Akuli commented Jan 25, 2025

I set up an awful Windows VM to debug this. (It is awful for various reasons.) This seems like a stack overflow bug.

I am adding debug prints to examples/aoc2024/day22/part2.jou, and running it with ../../../jou.exe -O0 part2.jou. It stops mid-way along a long loop, exits with status 1, and prints no error. Like this:

screenshot-1737848850

But if I add a dummy variable foo: byte[10000] on the stack, it stops earlier:

screenshot-1737848939

I think the problem is that the new builder is allocating stack space in the loop, instead of doing it before the loop and then reusing the stack variables. I'm glad the test setup is good enough to catch this :)

@Akuli
Copy link
Owner Author

Akuli commented Jan 26, 2025

Apparently using -o and then double-clicking the executable gives a better error message on Windows.

screenshot-1737849566

screenshot-1737849728

screenshot-1737849754

Due to my terrible VM setup, I cannot copy/paste or copy files from the VM, so anyone else reading this will be stuck with Finnish screenshots. Oops :)

@Akuli
Copy link
Owner Author

Akuli commented Jan 26, 2025

It is a stack overflow https://stackoverflow.com/a/76892354

I wonder if there's a way to better report this kind of errors to the Jou programmer when not using -o.

@Akuli
Copy link
Owner Author

Akuli commented Jan 26, 2025

It worked!!!! :)

@Akuli Akuli merged commit 3e0e5d8 into main Jan 26, 2025
38 checks passed
@Akuli Akuli deleted the builder-refactor branch January 26, 2025 00:20
@Akuli Akuli added the enhancement New feature or request label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big A lot of work to implement enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant