|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: Compiler Phases |
| 4 | +--- |
| 5 | + |
| 6 | +As described in the [compiler overview](lifecycle.md#phases), `dotc` is divided into a list of [phases][Phase], |
| 7 | +specified in the [Compiler] class. |
| 8 | + |
| 9 | +#### Printing the phases of the Compiler |
| 10 | + |
| 11 | +a flattened list of all the phases can be displayed by invoking |
| 12 | +the compiler with the `-Xshow-phases` flag: |
| 13 | +``` |
| 14 | +$ scalac -Xshow-phases |
| 15 | +``` |
| 16 | + |
| 17 | +## Phase Groups |
| 18 | + |
| 19 | +In class [Compiler] you can access the list of phases with the method `phases`: |
| 20 | + |
| 21 | +```scala |
| 22 | +def phases: List[List[Phase]] = |
| 23 | + frontendPhases ::: picklerPhases ::: transformPhases ::: backendPhases |
| 24 | +``` |
| 25 | + |
| 26 | +You can see that phases are actually grouped into sublists, given by the signature |
| 27 | +`List[List[Phase]]`; that is, each sublist forms a phase group that is then *fused* into a |
| 28 | +single tree traversal when a [Run] is executed. |
| 29 | + |
| 30 | +Phase fusion allows each phase of a group to be small and modular, |
| 31 | +(each performing a single function), while reducing the number of tree traversals |
| 32 | +and increasing performance. |
| 33 | + |
| 34 | +Phases are able to be grouped together if they inherit from [MiniPhase]. |
| 35 | + |
| 36 | +## Phase Categories |
| 37 | + |
| 38 | +Phases fall into four categories, allowing customisation by sub-classes of [Compiler]: |
| 39 | + |
| 40 | +### `frontendPhases` |
| 41 | +In the main compiler these include [parser], [typer], [posttyper], |
| 42 | +[prepjsinterop] and phases for producing SemanticDB and communicating with the |
| 43 | +incremental compiler Zinc. |
| 44 | +The [parser] reads source programs and generates untyped abstract syntax trees, which |
| 45 | +in [typer] are then typechecked and transformed into typed abstract syntax trees. |
| 46 | +Following is [posttyper], performing checks and cleanups that require a fully typed program. |
| 47 | +In particular, it |
| 48 | +- creates super accessors representing `super` calls in traits |
| 49 | +- creates implementations of compiler-implemented methods, |
| 50 | +such as `equals` and `hashCode` for case classes. |
| 51 | +- marks [compilation units][CompilationUnit] that require inline expansion, or quote pickling |
| 52 | +- simplifies trees of erased definitions |
| 53 | +- checks variance of type parameters |
| 54 | +- mark parameters passed unchanged from subclass to superclass for later pruning. |
| 55 | + |
| 56 | +### `picklerPhases` |
| 57 | +These phases start with [pickler], which serializes typed trees |
| 58 | +produced by the `frontendPhases` into TASTy format. Following is [inlining], |
| 59 | +which expand calls to inline methods, and [postInlining] providing implementations |
| 60 | +of the [Mirror] framework for inlined calls. |
| 61 | +Finally are [staging], which ensures that quotes conform to the |
| 62 | +[Phase Consistency Principle (PCP)][PCP], and [pickleQuotes] which converts quoted |
| 63 | +trees to embedded TASTy strings. |
| 64 | + |
| 65 | +### `transformPhases` |
| 66 | +These phases are concerned with tranformation into lower-level forms |
| 67 | +suitable for the runtime system, with two sub-groupings: |
| 68 | +- High-level transformations: All phases from [firstTransform] to [erasure]. |
| 69 | + Most of these phases transform syntax trees, expanding high-level constructs |
| 70 | + to more primitive ones. |
| 71 | + - An important transform phase is [patternMatcher], which converts match |
| 72 | + trees and patterns into lower level forms, as well as checking the |
| 73 | + exhaustivity of sealed types, and unreachability of pattern cases. |
| 74 | + - Some phases perform further checks on more primitive trees, |
| 75 | + e.g. [refchecks] verifies that no abstract methods exist in concrete classes, |
| 76 | + and [initChecker] checks that fields are not used before initialisation. |
| 77 | + - The last phase in the group, [erasure] translates all |
| 78 | + types into types supported directly by the JVM. To do this, it performs |
| 79 | + another type checking pass, but using the rules of the JVM's type system |
| 80 | + instead of Scala's. |
| 81 | +- Low-level transformations: All phases from `ElimErasedValueType` to |
| 82 | + `CollectSuperCalls`. These further transform trees until they are essentially a |
| 83 | + structured version of Java bytecode. |
| 84 | + |
| 85 | +### `backendPhases` |
| 86 | +These map the transformed trees to Java classfiles or SJSIR files. |
| 87 | + |
| 88 | +[CompilationUnit]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/CompilationUnit.scala |
| 89 | +[Compiler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Compiler.scala |
| 90 | +[Phase]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/core/Phases.scala |
| 91 | +[MiniPhase]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/MegaPhase.scala |
| 92 | +[Run]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/Run.scala |
| 93 | +[parser]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/parsing/ParserPhase.scala |
| 94 | +[typer]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/typer/TyperPhase.scala |
| 95 | +[posttyper]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PostTyper.scala |
| 96 | +[prepjsinterop]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala |
| 97 | +[pickler]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Pickler.scala |
| 98 | +[inlining]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Inlining.scala |
| 99 | +[postInlining]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PostInlining.scala |
| 100 | +[staging]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Staging.scala |
| 101 | +[pickleQuotes]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala |
| 102 | +[refchecks]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/typer/RefChecks.scala |
| 103 | +[initChecker]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/init/Checker.scala |
| 104 | +[firstTransform]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala |
| 105 | +[patternMatcher]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala |
| 106 | +[erasure]: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/Erasure.scala |
| 107 | +[Mirror]: https://github.com/lampepfl/dotty/blob/master/library/src/scala/deriving/Mirror.scala |
| 108 | +[PCP]: {{ site.scala3ref }}/metaprogramming/macros.html#the-phase-consistency-principle |
0 commit comments