• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ahinchman1/Kotlin-Compiler-Crash-Course: A repository of helpful sources to figu ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

ahinchman1/Kotlin-Compiler-Crash-Course

开源软件地址(OpenSource Url):

https://github.com/ahinchman1/Kotlin-Compiler-Crash-Course

开源编程语言(OpenSource Language):


开源软件介绍(OpenSource Introduction):

Kotlin-Compiler-Crash-Course

A repository of helpful sources to figure out what the Kotlin compiler really is

Alt Text

  • 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

Alt Text

Phases of the Frontend

Lexer

The lexer phase breaks source code text into a sequence of lexical tokens:

  • KotlinLexer.g4
  • This phase can then split into 2 phases:
    • Scanning: segments input text into syntactic units and assign them a category
    • Evaluating: converts lexemes into a processed val

Parser

PSI/AST trees

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),

Semantic analysis

Middle-end phases of the Compiler

  • Also known as the optimizer, performs optimizations on the intermediate representation
  • Analysis: data analysis from the intermediate representation
    • Data-flow analysis
    • Alias analysis
    • Pointer analysis
    • Escape analysis
  • Accurate analysis is the basis for any compiler optimization
    • Control flow graph
    • Call graph
  • Complier analysis is a prerequisite for any compiler optimization

Backend of the Complier

  • Responsible for the CPU architecture-specific optimizations and for code generation
  • Machine-dependent optimizations: optimizations that depend on the details of the CPU architecture that the compiler targets
  • Code Generation: transforms IR into the output language, usually the native machine language of the ysstem
    • Resource and storage decisions

More resources around the Kotlin Compiler




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap