AdaCore / AdaCore/langkit

Proposal: Nanopass compiler support

Open
#668 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ada
Stars
94
Forks
34
PR merge metrics
No merged PRs in 30d

Description

This is a proposal for adding support for building Nanopass compilers to Langkit (most likely in a fork rather than something that will ever be upstreamed). I'm looking to get some feedback on this from other Langkit users to help refine this idea before I start implementing this. For some background on Nanopass compilers, refer to the following:

In short, from the second document:

The nanopass framework provides a tool for writing compilers composed of several simple passes that operate over well-defined intermediate languages. The goal of this organization is both to simplify the understanding of each pass, because it is responsible for a single task, and to simplify the addition of new passes anywhere in the compiler.

Currently it is entirely possible to write a Nanopass compiler using Langkit by manually writing a series of transformations, however this involves a lot of boilerplate, especially if one wants to formally define and check intermediate languages. I propose the addition of a set of new Langkit constructs to easily and formally define intermediate languages, pipelines, and transformers. I imagine this would look something like the following:

language parsed_foolang from grammar foolang_grammar;

language flat_foolang extends parsed_foolang {
    remove BlockStatement;
}

pass flatten_blocks : parsed_foolang -> flat_foolang
    with Libfoolang.Nanopass.Passes.Flatten_Blocks;

where Libfoolang.Nanopass.Passes.Flatten_Blocks is an Ada package with a Transform functions which takes some kind of rewriting handle. Langkit would generate the stub for this package.

In addition to remove, which makes a node invalid to have in a language, we would also have add to make a node valid and replace to change the shape of a node. I'm not sure exactly what this would look like yet.

Langkit would also be able to generate a pipeline function which calls a series of passes and checks that the outputs are valid between then.

That's about all I have on this idea for now, I plan to start prototyping it after I finish some other projects. Any ideas on how to design this are welcome.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposal and its linked Nanopass dissertation and Racket documentation; the issue names no Langkit files, tests, or entry points. Done would require an agreed design for intermediate languages, passes, transformations, validation, pipelines, and generated Ada stubs before implementation can be scoped.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.