CodeBlanks Logo Icon CodeBlanks

Compilation pipeline

Learn the Dart compilation pipeline.

Compilation pipeline

Compilation pipeline is the set of stages that turn Dart source code into machine code or an executable form. The source is first tokenized, then parsed into an AST (abstract syntax tree). The AST is compiled into Kernel, an intermediate representation (IR).

Kernel is sent to backends: dart2js for the web, dart native (AOT) for native. Each backend generates code for its target platform. This flow lets the same source compile to multiple platforms. Each stage consumes the previous stage's output.

Kernel and backend

Kernel is Dart's platform-independent intermediate representation. It can be serialized as a .dill file. All backends consume Kernel; this way the frontend is written once and multiple targets are supported.

Backends: dart2js (JavaScript), dart2wasm (WebAssembly), dart native (AOT machine code), and the VM (JIT). Each backend uses its own code generation strategy. Kernel is the single 'verified' form of the language.

Practice A pipeline Phase class

Define a Phase class with final String name; final String output; Phase(this.name, this.output); field and constructor. Add a String describe() => '$name → $output'; method. In main create final p = Phase('Parse', 'AST');; print with print(p.describe());.

Starter code

main.dart
void main() {
  // define a Phase class (name, output; describe method)
  // create a Phase; print describe()
}

Sample solution

main.dart
class Phase {
  final String name;
  final String output;
  Phase(this.name, this.output);
  String describe() => '$name → $output';
}

void main() {
  final p = Phase('Parse', 'AST');
  print(p.describe());
}

Expected output

Parse → AST

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is one of the main stages of the compilation pipeline?

  • Only garbage collection
  • Only package download
  • Only UI render
  • Parsing to AST Correct answer

Explanation: parsing source code into AST is a core pipeline stage.

2 A pipeline stage

Question
final phase = '____';
print(phase);
Answer
final phase = 'kernel';
print(phase);

Explanation: kernel is the intermediate form produced after AST.

3 Match the pipeline stages

Term Meaning
Tokenize Token stream
Parse AST production
Kernel Intermediate (IR)
Backend Target code generation

All lessons in Expert