Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log injection #16

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions knee-compiler-plugin/src/main/kotlin/Interfaces.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down
31 changes: 25 additions & 6 deletions knee-compiler-plugin/src/main/kotlin/context/KneeLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,25 +28,41 @@ class KneeLogger(
if (verboseLogs) println(message)
}

private var printlnIr: IrSimpleFunctionSymbol? = null
private val printlnCodegen = MemberName("kotlin", "println")
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", "export")
printlnIr = function.single { it.owner.valueParameters.firstOrNull()?.type == builtIns.stringType }
val function = builtIns.findFunctions(Name.identifier("println"), "kotlin", "io")
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
Expand Down
2 changes: 2 additions & 0 deletions knee-gradle-plugin/src/main/kotlin/KneePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ internal open class JvmInterfaceWrapper<T: Any>(
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)
Expand Down