Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Source to Bytecode: Understanding the Kotl...

From Source to Bytecode: Understanding the Kotlin Compilation Process

Dive into the inner workings of the Kotlin compiler in this informative session. We'll explore how Kotlin code is transformed into JVM bytecode, covering key stages such as lexical analysis, parsing, semantic analysis, intermediate representation (IR) and code generation. Ideal for Kotlin developers and software engineers, this talk will enhance your understanding of the compilation process and help you write more efficient code. Join us to decode the journey from source to bytecode in Kotlin!

Theophilus Kibet

November 08, 2024
Tweet

More Decks by Theophilus Kibet

Other Decks in Technology

Transcript

  1. A program which translates a program in a source language

    to a program in target language. . What is a compiler? P. 04 Bemefits OF Coroutines Source Code Compiler Target/machine code
  2. 01 P. 04 Bemefits OF Coroutines .kt files Kotlin Compiler

    Kotlin Compiler JVM Bytecode JavaScript LLVM Bitcode Wasm
  3. 03 P. 04 Bemefits OF Coroutines .kt Kotlin Compiler backend

    target code lexer parser semantic analyzer tokens syntax tree syntax tree + semantic info
  4. 04 P. 04 Bemefits OF Coroutines .kt Kotlin Compiler frontend

    target code intermediate code generator & optimizer machine code generator & optimizer syntax tree + semantic info intermidiate representation
  5. Consumes plain text of a program and groups together individual

    characters to form complete tokens. Token - has a value and a token type. Lexer P. 04 fun getSpeaker(id: String) { }
  6. Consumes tokens and groups them together into complete statements and

    expressions, guided by the grammar. It outputs a syntax tree. Parser P. 04
  7. A set of rules defining the language structure. ‘fun’ identifier

    ‘(’ parameters ‘)’ ‘{’ statements ‘}’ NB:this is just a sample rule, Kotlin has many rules for functions. Grammar
  8. -> grammatical structure of the program. Abstract syntax tree(AST) contains

    only information which are needed for processing the source input. Concrete syntax tree(CST) contains all the information derived from the source input, this includes things like whitespaces and comments. NB:Kotlin Compiler produces a CST, referred as Program Structure Interface(PSI). Syntax tree
  9. P. 04 Bemefits OF Coroutines .kt PSI2FIR backend target code

    lexer parser semantic analyzer tokens PSI syntax tree + semantic info Transforms PSI to FIR.
  10. -> Enhanced PSI designed for code resolution. -> Some optimization

    is done on the FIR. e.g replacing if statements with when and for loops with while Frontend Intermediate Representation Use FIR Tree plugin to view
  11. P. 04 Bemefits OF Coroutines .kt frontend target code intermediate

    code generator & optimizer machine code generator & optimizer FIR intermidiate representation FIR2IR Transforms FIR to IR.
  12. -> Enhanced FIR designed for code generation. -> More optimization

    is done on the IR, these are mostly architecture independent. Intermediate Representation
  13. Constant folding. converting an expression by combining multiple constants into

    a single constant. Strength Reduction. converting a special case of an expensive operation into a less expensive. e.g 3^3 becomes 3*3 Optimizations on the IR
  14. -> Hard to navigate and run the project. -> No

    resources resources on getting dumps for every stage, you have to write a compiler plugin. NB: You can get help on Kotlin Slack - #compiler Challenges
  15. 18 Resources Kotlin-Compiler-Crash-Course by Amanda What Everyone Must Know About

    The NEW Kotlin K2 Compiler Exploring Kotlin Performance with Romain Guy Kotlin-script-parser-test Kotlin Repository Compiler Explorer Kotlin Explorer