Slide 1

Slide 1 text

Writing Your First Kotlin Compiler Plugin Kevin Most

Slide 2

Slide 2 text

A brief intro

Slide 3

Slide 3 text

Are these basically annotation processors?

Slide 4

Slide 4 text

Are these basically annotation processors? • Annotation Processors:

Slide 5

Slide 5 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time

Slide 6

Slide 6 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API

Slide 7

Slide 7 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code

Slide 8

Slide 8 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code

Slide 9

Slide 9 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported

Slide 10

Slide 10 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins:

Slide 11

Slide 11 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins: • Your code runs at compile-time

Slide 12

Slide 12 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins: • Your code runs at compile-time • Private, undocumented API

Slide 13

Slide 13 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins: • Your code runs at compile-time • Private, undocumented API • Emit Java bytecode (or LLVM IR)

Slide 14

Slide 14 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins: • Your code runs at compile-time • Private, undocumented API • Emit Java bytecode (or LLVM IR) • Works on Kotlin source code only

Slide 15

Slide 15 text

Are these basically annotation processors? • Annotation Processors: • Your code runs at compile-time • Public, documented API • Emit Java source code • Works on Kotlin/Java source code • Multiplatform not supported • Compiler Plugins: • Your code runs at compile-time • Private, undocumented API • Emit Java bytecode (or LLVM IR) • Works on Kotlin source code only • Multiplatform supported

Slide 16

Slide 16 text

Why write compiler plugins?

Slide 17

Slide 17 text

Why write compiler plugins? • Incredibly powerful API; you can modify function/class internals

Slide 18

Slide 18 text

Why write compiler plugins? • Incredibly powerful API; you can modify function/class internals • Enables you to solve new classes of metaprogramming problems

Slide 19

Slide 19 text

Why write compiler plugins? • Incredibly powerful API; you can modify function/class internals • Enables you to solve new classes of metaprogramming problems • Annotation processors are JVM-only, while compiler plugins aren't

Slide 20

Slide 20 text

Why NOT write compiler plugins?

Slide 21

Slide 21 text

Why NOT write compiler plugins? • Annotation processors are much easier to write (if you only care about JVM)

Slide 22

Slide 22 text

Why NOT write compiler plugins? • Annotation processors are much easier to write (if you only care about JVM) • Compiler plugins are a lot of work. You need to write:

Slide 23

Slide 23 text

Why NOT write compiler plugins? • Annotation processors are much easier to write (if you only care about JVM) • Compiler plugins are a lot of work. You need to write: • An IntelliJ plugin (if creating synthetic members)

Slide 24

Slide 24 text

Why NOT write compiler plugins? • Annotation processors are much easier to write (if you only care about JVM) • Compiler plugins are a lot of work. You need to write: • An IntelliJ plugin (if creating synthetic members) • A Gradle (or Maven, or other build tool) plugin

Slide 25

Slide 25 text

Why NOT write compiler plugins? • Annotation processors are much easier to write (if you only care about JVM) • Compiler plugins are a lot of work. You need to write: • An IntelliJ plugin (if creating synthetic members) • A Gradle (or Maven, or other build tool) plugin • Slightly different extensions for JVM, JS, and Native targets

Slide 26

Slide 26 text

Examples of compiler plugins

Slide 27

Slide 27 text

Examples of compiler plugins • allopen: Modifies annotated class to be open

Slide 28

Slide 28 text

Examples of compiler plugins • allopen: Modifies annotated class to be open • noarg: Modifies annotated class to have a zero-argument constructor

Slide 29

Slide 29 text

Examples of compiler plugins • allopen: Modifies annotated class to be open • noarg: Modifies annotated class to have a zero-argument constructor • android-extensions: findViewById(R.id.foo) aliased, and automatic Parcelable impl generation via @Parcelize

Slide 30

Slide 30 text

Examples of compiler plugins • allopen: Modifies annotated class to be open • noarg: Modifies annotated class to have a zero-argument constructor • android-extensions: findViewById(R.id.foo) aliased, and automatic Parcelable impl generation via @Parcelize • kotlin-serialization: Automatic generation of Serializable impl

Slide 31

Slide 31 text

Examples of compiler plugins • allopen: Modifies annotated class to be open • noarg: Modifies annotated class to have a zero-argument constructor • android-extensions: findViewById(R.id.foo) aliased, and automatic Parcelable impl generation via @Parcelize • kotlin-serialization: Automatic generation of Serializable impl • First multiplatform-ready plugin (generates LLVM IR for native too)

Slide 32

Slide 32 text

Examples of compiler plugins

Slide 33

Slide 33 text

• All existing compiler plugins are 1st party (github.com/JetBrains/kotlin) Examples of compiler plugins

Slide 34

Slide 34 text

• All existing compiler plugins are 1st party (github.com/JetBrains/kotlin) • plugins/{name}/ ... for the actual plugin business logic Examples of compiler plugins

Slide 35

Slide 35 text

• All existing compiler plugins are 1st party (github.com/JetBrains/kotlin) • plugins/{name}/ ... for the actual plugin business logic • libraries/tools/kotlin-{name}/ ... for the Gradle wrappers Examples of compiler plugins

Slide 36

Slide 36 text

• All existing compiler plugins are 1st party (github.com/JetBrains/kotlin) • plugins/{name}/ ... for the actual plugin business logic • libraries/tools/kotlin-{name}/ ... for the Gradle wrappers • libraries/tools/kotlin-maven-{name}/ ... for the Maven wrappers Examples of compiler plugins

Slide 37

Slide 37 text

Plugin Architecture s Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 38

Slide 38 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 39

Slide 39 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 40

Slide 40 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Gradle API (totally unrelated to Kotlin)

Slide 41

Slide 41 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Gradle API (totally unrelated to Kotlin) • Provides an entry point from a build.gradle script

Slide 42

Slide 42 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Gradle API (totally unrelated to Kotlin) • Provides an entry point from a build.gradle script • Allows configuration via Gradle extensions

Slide 43

Slide 43 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 44

Slide 44 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • The interface between the Gradle and Kotlin APIs

Slide 45

Slide 45 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • The interface between the Gradle and Kotlin APIs • Read Gradle extension options

Slide 46

Slide 46 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • The interface between the Gradle and Kotlin APIs • Read Gradle extension options • Write out Kotlin SubpluginOptions

Slide 47

Slide 47 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • The interface between the Gradle and Kotlin APIs • Read Gradle extension options • Write out Kotlin SubpluginOptions • Define the compiler plugin's ID (internal unique key)

Slide 48

Slide 48 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • The interface between the Gradle and Kotlin APIs • Read Gradle extension options • Write out Kotlin SubpluginOptions • Define the compiler plugin's ID (internal unique key) • Define the Kotlin plugin's Maven coordinates so the compiler can download it

Slide 49

Slide 49 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 50

Slide 50 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Reads kotlinc -Xplugin args

Slide 51

Slide 51 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Reads kotlinc -Xplugin args • Subplugin options actually get passed through this pipeline

Slide 52

Slide 52 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Reads kotlinc -Xplugin args • Subplugin options actually get passed through this pipeline • Write CompilerConfigurationKeys

Slide 53

Slide 53 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 54

Slide 54 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Read CompilerConfigurationKeys

Slide 55

Slide 55 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Read CompilerConfigurationKeys • Register Extensions

Slide 56

Slide 56 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 57

Slide 57 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!)

Slide 58

Slide 58 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as:

Slide 59

Slide 59 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as: • ExpressionCodegenExtension

Slide 60

Slide 60 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as: • ExpressionCodegenExtension • ClassBuilderInterceptorExtension

Slide 61

Slide 61 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as: • ExpressionCodegenExtension • ClassBuilderInterceptorExtension • StorageComponentContainerContributor

Slide 62

Slide 62 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as: • ExpressionCodegenExtension • ClassBuilderInterceptorExtension • StorageComponentContainerContributor • IrGenerationExtension ( !!)

Slide 63

Slide 63 text

Plugin Architecture Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension • Generates code (finally!) • Multiple types of extensions, such as: • ExpressionCodegenExtension • ClassBuilderInterceptorExtension • StorageComponentContainerContributor • IrGenerationExtension ( !!) • Write bytecode (or LLVM IR!!)

Slide 64

Slide 64 text

Let's build our own!

Slide 65

Slide 65 text

Let's build our own!

Slide 66

Slide 66 text

Let's build our own! • We'll build a compiler plugin that traces method calls

Slide 67

Slide 67 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging

Slide 68

Slide 68 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging • Could not be an annotation processor; modifies the function body

Slide 69

Slide 69 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging • Could not be an annotation processor; modifies the function body • Prior art:

Slide 70

Slide 70 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging • Could not be an annotation processor; modifies the function body • Prior art: • Hugo: github.com/jakewharton/hugo

Slide 71

Slide 71 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging • Could not be an annotation processor; modifies the function body • Prior art: • Hugo: github.com/jakewharton/hugo • Firebase Performance Monitoring: firebase.google.com/docs/perf-mon

Slide 72

Slide 72 text

Let's build our own! • We'll build a compiler plugin that traces method calls • A method annotated with a debug-log annotation will have its method body modified to include logging • Could not be an annotation processor; modifies the function body • Prior art: • Hugo: github.com/jakewharton/hugo • Firebase Performance Monitoring: firebase.google.com/docs/perf-mon • Both use AspectJ bytecode weaving + Android Gradle Transform API

Slide 73

Slide 73 text

fun prime(n: Int): Long { println("⇢ prime(n=$n)") val startTime = System.currentTimeMillis() val result = primeNumberSequence.take(n).last() val timeToRun = System.currentTimeMillis() - startTime println("⇠ prime [ran in $timeToRun ms]") return result } The goal

Slide 74

Slide 74 text

fun prime(n: Int): Long { println("⇢ prime(n=$n)") val startTime = System.currentTimeMillis() val result = primeNumberSequence.take(n).last() val timeToRun = System.currentTimeMillis() - startTime println("⇠ prime [ran in $timeToRun ms]") return result } The goal

Slide 75

Slide 75 text

@DebugLog fun prime(n: Int): Long = primeNumberSequence.take(n).last() fun prime(n: Int): Long { println("⇢ prime(n=$n)") val startTime = System.currentTimeMillis() val result = primeNumberSequence.take(n).last() val timeToRun = System.currentTimeMillis() - startTime println("⇠ prime [ran in $timeToRun ms]") return result } The goal

Slide 76

Slide 76 text

Let's build our own!

Slide 77

Slide 77 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 78

Slide 78 text

gradle-plugin/build.gradle apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" implementationClass = "debuglog.DebugLogGradlePlugin" } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 79

Slide 79 text

apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" implementationClass = "debuglog.DebugLogGradlePlugin" } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" } gradle-plugin/build.gradle

Slide 80

Slide 80 text

apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" // `apply plugin: "debuglog.plugin"` implementationClass = "debuglog.DebugLogGradlePlugin" } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" } gradle-plugin/build.gradle

Slide 81

Slide 81 text

apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" // `apply plugin: "debuglog.plugin"` implementationClass = "debuglog.DebugLogGradlePlugin" // entry-point class } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" } gradle-plugin/build.gradle

Slide 82

Slide 82 text

gradle-plugin/build.gradle apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" // `apply plugin: "debuglog.plugin"` implementationClass = "debuglog.DebugLogGradlePlugin" // entry-point class } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 83

Slide 83 text

gradle-plugin/build.gradle apply plugin: "java-gradle-plugin" apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" gradlePlugin { plugins { simplePlugin { id = "debuglog.plugin" // `apply plugin: "debuglog.plugin"` implementationClass = "debuglog.DebugLogGradlePlugin" // entry-point class } } } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" implementation "org.jetbrains.kotlin:kotlin-gradle-plugin-api:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 84

Slide 84 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 85

Slide 85 text

class DebugLogGradlePlugin : org.gradle.api.Plugin { override fun apply(project: Project) { project.extensions.create( "debugLog", DebugLogGradleExtension ::class.java ) } } open class DebugLogGradleExtension { var enabled: Boolean = true var annotations: List = emptyList() }

Slide 86

Slide 86 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 87

Slide 87 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = TODO() override fun getCompilerPluginId(): String = TODO() override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 88

Slide 88 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = TODO() override fun getCompilerPluginId(): String = TODO() override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 89

Slide 89 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = TODO() override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 90

Slide 90 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = TODO() override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 91

Slide 91 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = TODO() override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 92

Slide 92 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 93

Slide 93 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 94

Slide 94 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = TODO() override fun apply(project: Project, /* ... */): List { TODO() }2 }1

Slide 95

Slide 95 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact( groupId = "debuglog", artifactId = "kotlin-plugin", version = "0.0.1" ) override fun apply(project: Project, /* ... */): List { TODO() }2 }1

Slide 96

Slide 96 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin : KotlinGradleSubplugin { override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact( groupId = "debuglog", artifactId = "kotlin-plugin", version = "0.0.1" ) override fun apply(project: Project, /* ... */): List { TODO() } }

Slide 97

Slide 97 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 override fun isApplicable( project: Project, task: AbstractCompile ): Boolean = project.plugins.hasPlugin(DebugLogGradlePlugin ::class.java) override fun getCompilerPluginId(): String = "debuglog" override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact( groupId = "debuglog", artifactId = "kotlin-plugin", version = "0.0.1" ) override fun apply(project: Project, /* ... */): List1{ TODO() }2 }1

Slide 98

Slide 98 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 // other method impls override fun apply(project: Project, /* ... */): List1{ TODO()Z }2 }1

Slide 99

Slide 99 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 // other method impls override fun apply(project: Project, /* ... */): List1{ val extension = project.extensions.findByType() ?: DebugLogGradleExtension() if (extension.enabled && extension.annotations.isEmpty()) error("DebugLog is enabled, but no annotations were set") val annotationOptions = extension.annotations .map { SubpluginOption(key = "debugLogAnnotation", value = it) } val enabledOption = SubpluginOption( key = "enabled", value = extension.enabled.toString()) return annotationOptions + enabledOption }2 }1

Slide 100

Slide 100 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 // other method impls override fun apply(project: Project, /* ... */): List1{ val extension = project.extensions.findByType() ?: DebugLogGradleExtension() if (extension.enabled && extension.annotations.isEmpty()) error("DebugLog is enabled, but no annotations were set") val annotationOptions = extension.annotations .map { SubpluginOption(key = "debugLogAnnotation", value = it) } val enabledOption = SubpluginOption( key = "enabled", value = extension.enabled.toString()) return annotationOptions + enabledOption }2 }1

Slide 101

Slide 101 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 // other method impls override fun apply(project: Project, /* ... */): List1{ val extension = project.extensions.findByType() ?: DebugLogGradleExtension() if (extension.enabled && extension.annotations.isEmpty()) error("DebugLog is enabled, but no annotations were set") val annotationOptions = extension.annotations .map { SubpluginOption(key = "debugLogAnnotation", value = it) } val enabledOption = SubpluginOption( key = "enabled", value = extension.enabled.toString()) return annotationOptions + enabledOption }2 }1

Slide 102

Slide 102 text

@AutoService(KotlinGradleSubplugin ::class) // don't forget! class DebugLogGradleSubplugin1:1KotlinGradleSubplugin1{1 // other method impls override fun apply(project: Project, /* ... */): List1{ val extension = project.extensions.findByType() ?: DebugLogGradleExtension() if (extension.enabled && extension.annotations.isEmpty()) error("DebugLog is enabled, but no annotations were set") val annotationOptions = extension.annotations .map { SubpluginOption(key = "debugLogAnnotation", value = it) } val enabledOption = SubpluginOption( key = "enabled", value = extension.enabled.toString()) return annotationOptions + enabledOption }2 }1

Slide 103

Slide 103 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 104

Slide 104 text

kotlin-plugin/build.gradle apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 105

Slide 105 text

kotlin-plugin/build.gradle apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 106

Slide 106 text

kotlin-plugin/build.gradle apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 107

Slide 107 text

kotlin-plugin/build.gradle apply plugin: "org.jetbrains.kotlin.jvm" apply plugin: "kotlin-kapt" dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$ktVersion" compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$ktVersion" compileOnly "com.google.auto.service:auto-service:1.0-rc4" kapt "com.google.auto.service:auto-service:1.0-rc4" }

Slide 108

Slide 108 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 109

Slide 109 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = TODO() override val pluginOptions: Collection = listOf( ) override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 110

Slide 110 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = TODO() override val pluginOptions: Collection = listOf( ) override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 111

Slide 111 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( ) override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 112

Slide 112 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( ) override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 113

Slide 113 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( )P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() }2 }1

Slide 114

Slide 114 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() }2 }1

Slide 115

Slide 115 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 116

Slide 116 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) { TODO() } }

Slide 117

Slide 117 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) = when (option.name) { } }

Slide 118

Slide 118 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) = when (option.name) { "enabled" -> configuration.put(KEY_ENABLED, value.toBoolean()) }2 }1

Slide 119

Slide 119 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) = when (option.name) { "enabled" -> configuration.put(KEY_ENABLED, value.toBoolean()) "debugLogAnnotation" -> configuration.appendList(KEY_ANNOTATIONS, value) }2 }1

Slide 120

Slide 120 text

@AutoService(CommandLineProcessor ::class) class DebugLogCommandLineProcessor : CommandLineProcessor { override val pluginId: String = "debuglog" // same as ID from subplugin override val pluginOptions: Collection = listOf( CliOption("enabled", "", "whether plugin is enabled"), CliOption( "debugLogAnnotation", "", "debug-log annotation names", required = true, allowMultipleOccurrences = true))P override fun processOption( option: CliOption, value: String, configuration: CompilerConfiguration ) = when (option.name) { "enabled" -> configuration.put(KEY_ENABLED, value.toBoolean()) "debugLogAnnotation" -> configuration.appendList(KEY_ANNOTATIONS, value) else -> error("Unexpected config option ${option.name}") }2

Slide 121

Slide 121 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 122

Slide 122 text

@AutoService(ComponentRegistrar ::class) class DebugLogComponentRegistrar : ComponentRegistrar { override fun registerProjectComponents( project: MockProject, configuration: CompilerConfiguration ) { if (configuration[KEY_ENABLED] == false) { return } ClassBuilderInterceptorExtension.registerExtension( project, DebugLogClassGenerationInterceptor( debugLogAnnotations = configuration[KEY_ANNOTATIONS] ?: error("debuglog plugin requires at least one annotation class option passed to it") ) ) } }

Slide 123

Slide 123 text

@AutoService(ComponentRegistrar ::class) class DebugLogComponentRegistrar : ComponentRegistrar { override fun registerProjectComponents( project: MockProject, configuration: CompilerConfiguration ) { if (configuration[KEY_ENABLED] == false) { return } ClassBuilderInterceptorExtension.registerExtension( project, DebugLogClassGenerationInterceptor( debugLogAnnotations = configuration[KEY_ANNOTATIONS] ?: error("debuglog plugin requires at least one annotation class option passed to it") ) ) } }

Slide 124

Slide 124 text

Plugin Subplugin CommandLineProcessor ComponentRegistrar Extension Extension

Slide 125

Slide 125 text

class DebugLogClassGenerationInterceptor( val debugLogAnnotations: List ) : ClassBuilderInterceptorExtension { override fun interceptClassBuilderFactory( interceptedFactory: ClassBuilderFactory, bindingContext: BindingContext, diagnostics: DiagnosticSink ): ClassBuilderFactory = TODO() } kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassGenerationInterceptor.kt

Slide 126

Slide 126 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassGenerationInterceptor.kt class DebugLogClassGenerationInterceptor( val debugLogAnnotations: List ) : ClassBuilderInterceptorExtension { override fun interceptClassBuilderFactory( interceptedFactory: ClassBuilderFactory, bindingContext: BindingContext, diagnostics: DiagnosticSink ): ClassBuilderFactory = object: ClassBuilderFactory by interceptedFactory }1

Slide 127

Slide 127 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassGenerationInterceptor.kt class DebugLogClassGenerationInterceptor( val debugLogAnnotations: List ) : ClassBuilderInterceptorExtension { override fun interceptClassBuilderFactory( interceptedFactory: ClassBuilderFactory, bindingContext: BindingContext, diagnostics: DiagnosticSink ): ClassBuilderFactory = object: ClassBuilderFactory by interceptedFactory { override fun newClassBuilder(origin: JvmDeclarationOrigin) = DebugLogClassBuilder( annotations = debugLogAnnotations, delegateBuilder = interceptedFactory.newClassBuilder(origin)) } }1

Slide 128

Slide 128 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt private class DebugLogClassBuilder( val annotations: List, delegateBuilder: ClassBuilder ) : DelegatingClassBuilder(delegateBuilder) { override fun newMethod( origin: JvmDeclarationOrigin, access: Int, name: String, desc: String, signature: String?, exceptions: Array? ): MethodVisitor { }1 }2

Slide 129

Slide 129 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt private class DebugLogClassBuilder( val annotations: List, delegateBuilder: ClassBuilder ) : DelegatingClassBuilder(delegateBuilder) { override fun newMethod( origin: JvmDeclarationOrigin, ... ): MethodVisitor { }1 }2

Slide 130

Slide 130 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt private class DebugLogClassBuilder( val annotations: List, delegateBuilder: ClassBuilder ) : DelegatingClassBuilder(delegateBuilder) { override fun newMethod( origin: JvmDeclarationOrigin, ... ): MethodVisitor { val original = super.newMethod(origin, ...) val function = origin.descriptor as? FunctionDescriptor ?: return original if (annotations.none { descriptor.annotations.hasAnnotation(it) }) { return original } }1 }2

Slide 131

Slide 131 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt private class DebugLogClassBuilder( val annotations: List, delegateBuilder: ClassBuilder ) : DelegatingClassBuilder(delegateBuilder) { override fun newMethod( origin: JvmDeclarationOrigin, ... ): MethodVisitor { val original = super.newMethod(origin, ...) val function = origin.descriptor as? FunctionDescriptor ?: return original if (annotations.none { descriptor.annotations.hasAnnotation(it) }) { return original } return object : MethodVisitor(Opcodes.ASM5, original) { }3 }1 }2

Slide 132

Slide 132 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { }3

Slide 133

Slide 133 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).apply { TODO("on method entry") } }4 }3

Slide 134

Slide 134 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).apply { TODO("on method entry") } }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN /* void */, ARETURN /* object */, IRETURN /* int */ -> { InstructionAdapter(this).apply { TODO("on method exit") } } } super.visitInsn(opcode) } }3

Slide 135

Slide 135 text

What now?

Slide 136

Slide 136 text

What now? • You write bytecode

Slide 137

Slide 137 text

What now? • You write bytecode • Uses the ObjectWeb ASM API

Slide 138

Slide 138 text

What now? • You write bytecode • Uses the ObjectWeb ASM API • Neither related to ASM (assembly) or Web ASM (wasm) in any way

Slide 139

Slide 139 text

What now? • You write bytecode • Uses the ObjectWeb ASM API • Neither related to ASM (assembly) or Web ASM (wasm) in any way • An API for modifying JVM bytecode

Slide 140

Slide 140 text

What now? • You write bytecode • Uses the ObjectWeb ASM API • Neither related to ASM (assembly) or Web ASM (wasm) in any way • An API for modifying JVM bytecode • The JVM is a stack machine

Slide 141

Slide 141 text

What now? • You write bytecode • Uses the ObjectWeb ASM API • Neither related to ASM (assembly) or Web ASM (wasm) in any way • An API for modifying JVM bytecode • The JVM is a stack machine • One stack that methods operate upon

Slide 142

Slide 142 text

What now? • You write bytecode • Uses the ObjectWeb ASM API • Neither related to ASM (assembly) or Web ASM (wasm) in any way • An API for modifying JVM bytecode • The JVM is a stack machine • One stack that methods operate upon • You can also read arbitrary variables from the Local Variable Array

Slide 143

Slide 143 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") }

Slide 144

Slide 144 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 145

Slide 145 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 146

Slide 146 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 147

Slide 147 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V return value of v1()

Slide 148

Slide 148 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V return value of v1()

Slide 149

Slide 149 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V return value of v1() return value of v2()

Slide 150

Slide 150 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V return value of v1() return value of v2()

Slide 151

Slide 151 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 152

Slide 152 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V v1() + v2()

Slide 153

Slide 153 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V v1() + v2()

Slide 154

Slide 154 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 155

Slide 155 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 156

Slide 156 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 157

Slide 157 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 158

Slide 158 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 159

Slide 159 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 160

Slide 160 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder StringBuilder

Slide 161

Slide 161 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder StringBuilder

Slide 162

Slide 162 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 163

Slide 163 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 164

Slide 164 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder "sum of values was "

Slide 165

Slide 165 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder "sum of values was "

Slide 166

Slide 166 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 167

Slide 167 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 168

Slide 168 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 169

Slide 169 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 170

Slide 170 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder v1() + v2()

Slide 171

Slide 171 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder v1() + v2()

Slide 172

Slide 172 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 173

Slide 173 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 174

Slide 174 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 175

Slide 175 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream StringBuilder

Slide 176

Slide 176 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream

Slide 177

Slide 177 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream String

Slide 178

Slide 178 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V stdout PrintStream String

Slide 179

Slide 179 text

What does bytecode look like? fun printSimpleSum() { val sum = v1() + v2() println("sum of values was $sum") } INVOKESTATIC myapp/RunnerKt.v1 ()I INVOKESTATIC myapp/RunnerKt.v2 ()I IADD ISTORE 1 GETSTATIC j/l/System.out : Lj/io/PrintStream; NEW j/l/StringBuilder DUP INVOKESPECIAL j/l/StringBuilder. ()V LDC "sum of values was " INVOKEVIRTUAL j/l/StringBuilder.append (Lj/l/String;)Lj/l/StringBuilder; ILOAD 1 INVOKEVIRTUAL j/l/StringBuilder.append (I)Lj/l/StringBuilder; INVOKEVIRTUAL j/l/StringBuilder.toString ()Lj/l/String; INVOKEVIRTUAL j/io/PrintStream.println (Lj/l/String;)V

Slide 180

Slide 180 text

@DebugLog fun prime(n: Int): Long = primeNumberSequence.take(n).last() fun prime(n: Int): Long { println("⇢ prime(n=$n)") val startTime = System.currentTimeMillis() val result = primeNumberSequence.take(n).last() val timeToRun = System.currentTimeMillis() - startTime println("⇠ prime [ran in $timeToRun ms]") return result } Remember the goal

Slide 181

Slide 181 text

Back to our MethodVisitor!

Slide 182

Slide 182 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).apply { TODO("on method entry") } }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).apply { TODO("on method exit") } } } super.visitInsn(opcode) } }3

Slide 183

Slide 183 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).apply { TODO("on method entry") } } override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).apply { TODO("on method exit") } } } super.visitInsn(opcode) } }

Slide 184

Slide 184 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { TODO("on method entry") }1

Slide 185

Slide 185 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") }1

Slide 186

Slide 186 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") }1 stdout PrintStream

Slide 187

Slide 187 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") }1 stdout PrintStream

Slide 188

Slide 188 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") }1 stdout PrintStream StringBuilder

Slide 189

Slide 189 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() }1 stdout PrintStream StringBuilder

Slide 190

Slide 190 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() }1 stdout PrintStream StringBuilder StringBuilder

Slide 191

Slide 191 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") }1 stdout PrintStream StringBuilder StringBuilder

Slide 192

Slide 192 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") }1 stdout PrintStream StringBuilder

Slide 193

Slide 193 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") }1 stdout PrintStream StringBuilder

Slide 194

Slide 194 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") }1 stdout PrintStream StringBuilder "⇢ prime("

Slide 195

Slide 195 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") }1 stdout PrintStream StringBuilder "⇢ prime("

Slide 196

Slide 196 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") }1 stdout PrintStream

Slide 197

Slide 197 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") }1 stdout PrintStream

Slide 198

Slide 198 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") }1 stdout PrintStream StringBuilder

Slide 199

Slide 199 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") }1 stdout PrintStream StringBuilder

Slide 200

Slide 200 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder

Slide 201

Slide 201 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder

Slide 202

Slide 202 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder " n="

Slide 203

Slide 203 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder " n="

Slide 204

Slide 204 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream

Slide 205

Slide 205 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream

Slide 206

Slide 206 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder

Slide 207

Slide 207 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder

Slide 208

Slide 208 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder value of n

Slide 209

Slide 209 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder value of n

Slide 210

Slide 210 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream

Slide 211

Slide 211 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream

Slide 212

Slide 212 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") } }1 stdout PrintStream StringBuilder

Slide 213

Slide 213 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R }1 stdout PrintStream StringBuilder

Slide 214

Slide 214 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }1 stdout PrintStream StringBuilder

Slide 215

Slide 215 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }1 stdout PrintStream

Slide 216

Slide 216 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }1 stdout PrintStream String

Slide 217

Slide 217 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }1 stdout PrintStream String

Slide 218

Slide 218 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply { getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }1

Slide 219

Slide 219 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L getstatic("j/l/System", "out", "Ljava/io/PrintStream;") anew("j/l/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇢ ${function.name}(") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/StringBuilder;") function.valueParameters.forEachIndexed { i, param -> visitLdcInsn(" ${param.name}=") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") visitVarInsn(ALOAD, i + 1) invokevirtual("j/l/StringBuilder", "append", "(Lj/l/Object;)Lj/l/SB;") }R invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }1

Slide 220

Slide 220 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L // ... method-trace-printing code }1

Slide 221

Slide 221 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L // ... method-trace-printing code invokestatic("j/l/System", "currentTimeMillis", "()J") }1

Slide 222

Slide 222 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L // ... method-trace-printing code invokestatic("j/l/System", "currentTimeMillis", "()J") }1 currentTimeMillis

Slide 223

Slide 223 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L // ... method-trace-printing code invokestatic("j/l/System", "currentTimeMillis", "()J") store(9001, LONG_TYPE) }1 currentTimeMillis

Slide 224

Slide 224 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).apply {L // ... method-trace-printing code invokestatic("j/l/System", "currentTimeMillis", "()J") store(9001, LONG_TYPE) }1

Slide 225

Slide 225 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyL{L // ... method-trace-printing code invokestatic("j/l/System", "currentTimeMillis", "()J") store(9001, LONG_TYPE) }1

Slide 226

Slide 226 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyL{L // ... method-trace-printing code // ... timestamp-storing code }W

Slide 227

Slide 227 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).applyL{L // ... method-trace-printing code // ... timestamp-storing code }W }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).apply { TODO("on method exit") } } } super.visitInsn(opcode) } }3

Slide 228

Slide 228 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).applyL{L // ... method-trace-printing code // ... timestamp-storing code }W }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).apply { TODO("on method exit") } } } super.visitInsn(opcode) } }3

Slide 229

Slide 229 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).apply { // ... method-trace-printing code // ... timestamp-storing code }W }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).applyM{MTODO("on method exit")M}M } } super.visitInsn(opcode) } }3

Slide 230

Slide 230 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M TODO("on method exit") }M

Slide 231

Slide 231 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") }M

Slide 232

Slide 232 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") }M stdout PrintStream

Slide 233

Slide 233 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") }M stdout PrintStream

Slide 234

Slide 234 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") }M stdout PrintStream StringBuilder

Slide 235

Slide 235 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() }M stdout PrintStream StringBuilder

Slide 236

Slide 236 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() }M stdout PrintStream StringBuilder StringBuilder

Slide 237

Slide 237 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") }M stdout PrintStream StringBuilder StringBuilder

Slide 238

Slide 238 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") }M stdout PrintStream StringBuilder

Slide 239

Slide 239 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") }M stdout PrintStream StringBuilder

Slide 240

Slide 240 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") }M stdout PrintStream StringBuilder "⇠ prime [ran in "

Slide 241

Slide 241 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") }M stdout PrintStream StringBuilder "⇠ prime [ran in "

Slide 242

Slide 242 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") }M stdout PrintStream

Slide 243

Slide 243 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") }M stdout PrintStream

Slide 244

Slide 244 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") }M stdout PrintStream StringBuilder

Slide 245

Slide 245 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") }M stdout PrintStream StringBuilder

Slide 246

Slide 246 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") }M stdout PrintStream StringBuilder current timestamp

Slide 247

Slide 247 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) }M stdout PrintStream StringBuilder current timestamp

Slide 248

Slide 248 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) }M stdout PrintStream StringBuilder current timestamp start timestamp

Slide 249

Slide 249 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) }M stdout PrintStream StringBuilder current timestamp start timestamp

Slide 250

Slide 250 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) }M stdout PrintStream StringBuilder

Slide 251

Slide 251 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) }M stdout PrintStream StringBuilder

Slide 252

Slide 252 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) }M stdout PrintStream StringBuilder (current - start) time

Slide 253

Slide 253 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") }M stdout PrintStream StringBuilder (current - start) time

Slide 254

Slide 254 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") }M stdout PrintStream

Slide 255

Slide 255 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") }M stdout PrintStream

Slide 256

Slide 256 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") }M stdout PrintStream StringBuilder

Slide 257

Slide 257 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") }M stdout PrintStream StringBuilder

Slide 258

Slide 258 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") }M stdout PrintStream StringBuilder " ms]"

Slide 259

Slide 259 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") }M stdout PrintStream StringBuilder " ms]"

Slide 260

Slide 260 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") }M stdout PrintStream

Slide 261

Slide 261 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") }M stdout PrintStream

Slide 262

Slide 262 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") }M stdout PrintStream StringBuilder

Slide 263

Slide 263 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }M stdout PrintStream StringBuilder

Slide 264

Slide 264 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }M stdout PrintStream

Slide 265

Slide 265 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") }M stdout PrintStream String

Slide 266

Slide 266 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }M stdout PrintStream String

Slide 267

Slide 267 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }M

Slide 268

Slide 268 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M getstatic("j/l/System", "out", "Lj/io/PrintStream;") anew("java/lang/StringBuilder") dup() invokespecial("j/l/StringBuilder", "", "()V") visitLdcInsn("⇠ ${function.name} [ran in ") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/StringBuilder;") invokestatic("j/l/System", "currentTimeMillis", "()J") load(9001, LONG_TYPE) sub(LONG_TYPE) invokevirtual("j/l/StringBuilder", "append", "(J)Lj/l/StringBuilder;") visitLdcInsn(" ms]") invokevirtual("j/l/StringBuilder", "append", "(Lj/l/String;)Lj/l/SB;") invokevirtual("j/l/StringBuilder", "toString", "()Lj/l/String;") invokevirtual("j/io/PrintStream", "println", "(Lj/l/String;)V") }M

Slide 269

Slide 269 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt InstructionAdapter(this).applyM{M //G ...Gbenchmark-printingGcode }M

Slide 270

Slide 270 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).applyL{L // ... method-trace-printing code // ... timestamp-storing code }W }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).applyM{M //G ...Gbenchmark-printingGcode }M } } super.visitInsn(opcode) } }3

Slide 271

Slide 271 text

kotlin-plugin/src/main/kotlin/debuglog/DebugLogClassBuilder.kt return object : MethodVisitor(Opcodes.ASM5, original) { override fun visitCode() { super.visitCode() InstructionAdapter(this).applyL{L // ... method-trace-printing code // ... timestamp-storing code }W }4 override fun visitInsn(opcode: Int) { when (opcode) { RETURN , ARETURN, IRETURN -> { InstructionAdapter(this).applyM{M //G ...Gbenchmark-printingGcode }M } } super.visitInsn(opcode) } }3

Slide 272

Slide 272 text

Done! Demo time!!!

Slide 273

Slide 273 text

Grab this code at: github.com/kevinmost/debuglog

Slide 274

Slide 274 text

Resources • https://github.com/JetBrains/kotlin/tree/master/plugins. Specifically: • noarg: One of the simplest ones • android-extensions: Good prior art for many of the extension types • kotlin-serialization: Newest one, documented well, generates LLVM • https://github.com/JetBrains/kotlin/tree/master/libraries/tools • All look pretty similar; noarg and allopen are the simplest to grok

Slide 275

Slide 275 text

Thank you! Kevin Most