CodeBlanks Logo Icon CodeBlanks

Kernel

Learn Dart Kernel and intermediate representation.

Kernel

Kernel is Dart's platform-independent intermediate representation (IR). It is produced from the AST and can be serialized as a .dill file. Kernel is the common input to all backends.

Kernel is in binary form; loading is fast. Kernel fully represents the language semantics: types, generics, nullability, metadata. When a Kernel file is loaded, the backend only does code generation; no parse/type check is needed again.

The binary form advantage

Since Kernel is in binary form, it loads much faster than text-based source code. This matters especially for large projects and incremental compilation. The dart run command uses cached .dill files.

Kernel is also the verified form of the language: type checking is done, semantic analysis is complete. Backends take this verified form and focus only on code generation. This reduces compile time.

Practice A KernelModule class

Define a KernelModule class with final String name; final bool verified; KernelModule(this.name, this.verified); field and constructor. Add a String status() => verified ? 'verified' : 'pending'; method. In main create final m = KernelModule('myapp', true);; print with print(m.status());.

Starter code

main.dart
void main() {
  // define a KernelModule class (name, verified; status method)
  // create a KernelModule; print status()
}

Sample solution

main.dart
class KernelModule {
  final String name;
  final bool verified;
  KernelModule(this.name, this.verified);
  String status() => verified ? 'verified' : 'pending';
}

void main() {
  final m = KernelModule('myapp', true);
  print(m.status());
}

Expected output

verified

Questions and answers

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

1 Match the Kernel parts

Term Meaning
Kernel Intermediate (IR)
.dill Binary file
AST Previous stage
Backend Next stage

2 What is Kernel used for?

  • Only for documentation
  • As a platform-independent IR Correct answer
  • For garbage collection
  • For UI rendering

Explanation: Kernel is the platform-independent IR that goes to all backends.

3 A .dill file

Question
final kernelFile = '____';
print(kernelFile);
Answer
final kernelFile = 'myapp.dill';
print(kernelFile);

Explanation: .dill is the binary file extension for Kernel.

All lessons in Expert