From aa50726478eb2ad738fceecf0a0c81c730b9bf17 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 1 Oct 2024 15:39:01 -0300 Subject: [PATCH 1/3] Verbose logging --- knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt b/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt index eb750f3..08a70b0 100644 --- a/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt +++ b/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt @@ -26,14 +26,14 @@ class KneeLogger( } private var printlnIr: IrSimpleFunctionSymbol? = null - private val printlnCodegen = MemberName("kotlin", "println") + private val printlnCodegen = MemberName("kotlin.io", "println") fun injectLog(scope: IrStatementsBuilder<*>, message: String) { if (!verboseRuntime) return if (printlnIr == null) { val builtIns = (scope.parent as IrDeclaration).file.module.irBuiltins - val function = builtIns.findFunctions(Name.identifier("println"), "kotlin", "export") + val function = builtIns.findFunctions(Name.identifier("println"), "kotlin", "io") printlnIr = function.single { it.owner.valueParameters.firstOrNull()?.type == builtIns.stringType } } @@ -44,7 +44,6 @@ class KneeLogger( } } - fun injectLog(scope: CodeBlock.Builder, message: String) { if (!verboseRuntime) return scope.addStatement("%M(%S)", printlnCodegen, "[KNEE_JVM] $message") From 1c754c7c06285f1c70d3937561a3e2fa17e399dc Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 1 Oct 2024 18:00:24 -0300 Subject: [PATCH 2/3] More logs and comments --- .../src/main/kotlin/Interfaces.kt | 7 +++++ .../src/main/kotlin/context/KneeLogger.kt | 28 ++++++++++++++++--- .../kotlin/compiler/JvmInterfaceWrapper.kt | 3 ++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/knee-compiler-plugin/src/main/kotlin/Interfaces.kt b/knee-compiler-plugin/src/main/kotlin/Interfaces.kt index 9b4b40a..a150c31 100644 --- a/knee-compiler-plugin/src/main/kotlin/Interfaces.kt +++ b/knee-compiler-plugin/src/main/kotlin/Interfaces.kt @@ -176,6 +176,10 @@ private fun KneeInterface.makeIrImplementationContents(context: KneeContext) { constructor.valueParameters += superConstructor.valueParameters[1].copyTo(constructor, defaultValue = null) // 1: jobject constructor.body = with(DeclarationIrBuilder(context.plugin, constructor.symbol)) { irBlockBody { + context.log.injectLog(this, "Calling super constructor") + // context.log.injectLog(this, CodegenType.from(sourceConcreteType).jvmClassName) + // context.log.injectLog(this, CodegenType.from(irImplementation.defaultType).jvmClassName) + +irDelegatingConstructorCall(superConstructor).apply { putValueArgument(0, irGet(constructor.valueParameters[0])) putValueArgument(1, irGet(constructor.valueParameters[1])) @@ -197,6 +201,7 @@ private fun KneeInterface.makeIrImplementationContents(context: KneeContext) { } )) } + context.log.injectLog(this, "Called super constructor, init self") +IrInstanceInitializerCallImpl(startOffset, endOffset, irImplementation.symbol, context.symbols.builtIns.unitType) } } @@ -274,6 +279,8 @@ class InterfaceCodec( returnType = interfaceImplClass.defaultType, content = { irContext.logger.injectLog(this, "$logPrefix INSTANTIATING the implementation class") + // irContext.logger.injectLog(this, irContext.environment) + // irContext.logger.injectLog(this, jni) +irReturn(irCallConstructor(interfaceImplClass.primaryConstructor!!.symbol, emptyList()).apply { putValueArgument(0, irGet(irContext.environment)) // environment putValueArgument(1, irGet(jni)) // jobject diff --git a/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt b/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt index 08a70b0..ceed78b 100644 --- a/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt +++ b/knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt @@ -6,7 +6,10 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.declarations.IrDeclaration +import org.jetbrains.kotlin.ir.declarations.IrValueDeclaration +import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol +import org.jetbrains.kotlin.ir.types.makeNullable import org.jetbrains.kotlin.ir.util.file import org.jetbrains.kotlin.name.Name @@ -25,25 +28,42 @@ class KneeLogger( if (verboseLogs) println(message) } - private var printlnIr: IrSimpleFunctionSymbol? = null + private var printlnIrString: IrSimpleFunctionSymbol? = null + private var printlnIrAny: IrSimpleFunctionSymbol? = null private val printlnCodegen = MemberName("kotlin.io", "println") fun injectLog(scope: IrStatementsBuilder<*>, message: String) { if (!verboseRuntime) return - if (printlnIr == null) { + if (printlnIrString == null) { val builtIns = (scope.parent as IrDeclaration).file.module.irBuiltins val function = builtIns.findFunctions(Name.identifier("println"), "kotlin", "io") - printlnIr = function.single { it.owner.valueParameters.firstOrNull()?.type == builtIns.stringType } + printlnIrString = function.single { it.owner.valueParameters.firstOrNull()?.type == builtIns.stringType } } with(scope) { - +irCall(printlnIr!!).apply { + +irCall(printlnIrString!!).apply { putValueArgument(0, scope.irString("[KNEE_KN] $message")) } } } + fun injectLog(scope: IrStatementsBuilder<*>, objToPrint: IrValueDeclaration) { + if (!verboseRuntime) return + + if (printlnIrAny == null) { + val builtIns = (scope.parent as IrDeclaration).file.module.irBuiltins + val function = builtIns.findFunctions(Name.identifier("println"), "kotlin", "io") + printlnIrAny = function.single { it.owner.valueParameters.firstOrNull()?.type == builtIns.anyType.makeNullable() } + } + + with(scope) { + +irCall(printlnIrAny!!).apply { + putValueArgument(0, irGet(objToPrint)) + } + } + } + fun injectLog(scope: CodeBlock.Builder, message: String) { if (!verboseRuntime) return scope.addStatement("%M(%S)", printlnCodegen, "[KNEE_JVM] $message") diff --git a/knee-runtime/src/backendMain/kotlin/compiler/JvmInterfaceWrapper.kt b/knee-runtime/src/backendMain/kotlin/compiler/JvmInterfaceWrapper.kt index 7cdcb39..b95f68d 100644 --- a/knee-runtime/src/backendMain/kotlin/compiler/JvmInterfaceWrapper.kt +++ b/knee-runtime/src/backendMain/kotlin/compiler/JvmInterfaceWrapper.kt @@ -42,6 +42,9 @@ internal open class JvmInterfaceWrapper( private val toString: jmethodID init { val jclass = ClassIds.get(environment, interfaceFqn) + // Got errors on API22 here: equals can't be found on interfaces and even worse, when it throws NoSuchMethodError and + // we try to create KneeJvmExceptionToken, instantiating it fails - instantiating any class extending throwable fails. + // Not sure if these are fixed and whether they are API level dependent. equals = MethodIds.get(environment, interfaceFqn, "equals", "(Ljava/lang/Object;)Z", false, jclass) hashCode = MethodIds.get(environment, interfaceFqn, "hashCode", "()I", false, jclass) toString = MethodIds.get(environment, interfaceFqn, "toString", "()Ljava/lang/String;", false, jclass) From 5586e9035c317fd9a23ec107502d80237195ed3b Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Tue, 22 Oct 2024 21:28:23 -0300 Subject: [PATCH 3/3] Expose Group --- knee-gradle-plugin/src/main/kotlin/KneePlugin.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/knee-gradle-plugin/src/main/kotlin/KneePlugin.kt b/knee-gradle-plugin/src/main/kotlin/KneePlugin.kt index 3fd36d2..ec71b0f 100644 --- a/knee-gradle-plugin/src/main/kotlin/KneePlugin.kt +++ b/knee-gradle-plugin/src/main/kotlin/KneePlugin.kt @@ -24,6 +24,8 @@ class KneePlugin : KotlinCompilerPluginSupportPlugin { companion object { @Suppress("ConstPropertyName") const val Version = KneeVersion + @Suppress("ConstPropertyName") + const val Group = KneeGroup } override fun apply(target: Project) {