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