A repository of helpful sources to figure out what the Kotlin compiler really is
A compiler is a computer program that translates computer code written in one programming language into another language: the target language.
Normally used for programs that translate from one higher language to a lower-level language to create an executable program
The frontend verifies syntax and semantics. For statically-typed languages, it performs type-checking by collecting type information
If the input program is syntactically incorrect, or has a type error, it generates error and/or warning messages, usually identifying the location in the source code
Other aspects of the frontend:
Lexical analysis
Syntax analysis
Semantic analysis
The frontend transforms the input program into an intermediate representation
The middle-end performs optimizations on IR that are independent of the CPU architecture being targets
i.e. removing dead code, unreachable code, discovery and propagation of constant value, refactoring
The back-end takes the optimized IR the middle end. It may perform more analysis, transformations + optimizations that are specific for the target CPU architecture
Responsible for the multithreading and other parallel processing
Phases of the Frontend
Lexer
The lexer phase breaks source code text into a sequence of lexical tokens:
According to the Kotlin compiler, a PSI, or Program Structure Interface, tree is built on top of the AST, adding semantics and methods for manipulating specific language constructs.
The AST nodes have a direct mapping to text ranges in the underlying document. The bottom-most nodes of the AST match individual tokens returned by the lexer, and higher level nodes match multiple-token fragments. Operations performed on nodes of the AST tree, such as inserting, removing, reordering nodes and so on, are immediately reflected as changes to the text of the underlying document (Implementing Parser and PSI).
The AST is used intensively during semantic analysis, where the compiler checks for correct usage of the elements of the program and the language. It also describes an abstracted representation of what a user writes in Kotlin. AST allows us to change the surface syntax of the language without changing the rest of the compiler (although that rarely happens),
请发表评论