Slide 1

Slide 1 text

From Source to Bytecode: Understanding the Kotlin Compilation Process Presented by Theophilus Kibet Android Engineer

Slide 2

Slide 2 text

What is a compiler? Kotlin Compiler Overview Resources What's Up today? Questions

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

01 P. 04 Bemefits OF Coroutines .kt files Kotlin Compiler Kotlin Compiler JVM Bytecode JavaScript LLVM Bitcode Wasm

Slide 5

Slide 5 text

02 P. 04 Bemefits OF Coroutines .kt Kotlin Compiler frontend backend target code

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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) { }

Slide 9

Slide 9 text

fun KtModifierKeywordToken.FUN getSpeaker KtToken.IDENTIFIER ( KtSingleValueToken.LPAR id KtToken.IDENTIFIER : KtSingleValueToken.COLON and so on..... Tokens

Slide 10

Slide 10 text

Consumes tokens and groups them together into complete statements and expressions, guided by the grammar. It outputs a syntax tree. Parser P. 04

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Element.FUN Token.fun Element.IDENTIFIER Element.VALUE_PARAMETER_ LIST Token.LBRACE Token.RBRACE Token.LPAR Token.RPAR Element.VALUE_PARAMETER Element.IDENTIFIER Token.COLON Element.TYPE_REFERENCE fun getSpeaker(id: String) { }

Slide 14

Slide 14 text

fun getSpeaker(id: String) { } Use PSIViewer plugin to view

Slide 15

Slide 15 text

P. 04 Bemefits OF Coroutines .kt PSI2FIR backend target code lexer parser semantic analyzer tokens PSI syntax tree + semantic info Transforms PSI to FIR.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Resolves Semantic info. imports. types Checks errors and report. Semantic Analyzer

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

-> Enhanced FIR designed for code generation. -> More optimization is done on the IR, these are mostly architecture independent. Intermediate Representation

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Compiler Explorer

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Thank @_kibetheophilus Theophilus Kibet You Contact kibettheophilus