From 7a0d0ec5a7095564bd12d38193864ad6557b51b1 Mon Sep 17 00:00:00 2001 From: Konstantin Anisimov Date: Fri, 25 Nov 2016 12:00:44 +0300 Subject: [PATCH] CODEGEN: emprovements: - fixed stop on non body functions - IMPLICIT_CAST implemented - naive extension function implementation (cherry picked from commit 3d3b51b0fca3980012e77f47feded5030f0d7901) --- .../kotlin/backend/konan/llvm/CodeGenerator.kt | 14 +++++++------- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 7 ++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index cd46eaeed32..1219a917211 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -15,16 +15,16 @@ internal class CodeGenerator(override val context:Context) : ContextUtils { var currentFunction:FunctionDescriptor? = null fun function(declaration: IrFunction) { - if (declaration.body == null) { - // Abstract and external methods. - return - } + assert(declaration.body != null) - if (currentFunction == declaration.descriptor) return + val descriptor = declaration.descriptor + if (currentFunction == descriptor) return val fn = prolog(declaration) var indexOffset = 0 - if (declaration.descriptor is ClassConstructorDescriptor || declaration.descriptor.dispatchReceiverParameter != null) { + if (descriptor is ClassConstructorDescriptor + || descriptor.dispatchReceiverParameter != null + || descriptor.extensionReceiverParameter != null) { val name = "this" val type = pointerType(LLVMInt8Type()); val v = alloca(type, name) @@ -33,7 +33,7 @@ internal class CodeGenerator(override val context:Context) : ContextUtils { indexOffset = 1; } - declaration.descriptor.valueParameters.forEachIndexed { i, descriptor -> + descriptor.valueParameters.forEachIndexed { i, descriptor -> val name = descriptor.name.asString() val type = descriptor.type val v = alloca(type, name) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 84118c2ff11..02ca5ae7849 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -233,7 +233,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid override fun visitFunction(declaration: IrFunction) { logger.log("visitFunction : ${ir2string(declaration)}") - if (declaration.descriptor.modality == Modality.ABSTRACT) + if (declaration.descriptor.modality == Modality.ABSTRACT || declaration.body == null) return codegen.function(declaration) metadator.function(declaration) @@ -414,7 +414,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid val variable = codegen.variable(value.descriptor.name.asString()) return codegen.load(variable!!, tmpVariableName) } - is LazyClassReceiverParameterDescriptor -> { + is LazyClassReceiverParameterDescriptor, + is ReceiverParameterDescriptor -> { if (value.descriptor.name.asString() == "") { return codegen.load(codegen.thisVariable(), tmpVariableName) } @@ -450,7 +451,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid private fun evaluateTypeOperator(tmpVariableName: String, value: IrTypeOperatorCall): LLVMValueRef? { when (value.operator) { IrTypeOperator.CAST -> return evaluateCast(tmpVariableName, value) - IrTypeOperator.IMPLICIT_CAST -> TODO("${ir2string(value)}") + IrTypeOperator.IMPLICIT_CAST -> return evaluateExpression(tmpVariableName, value.argument) IrTypeOperator.IMPLICIT_NOTNULL -> TODO("${ir2string(value)}") IrTypeOperator.IMPLICIT_COERCION_TO_UNIT -> return evaluateExpression(tmpVariableName, value.argument) IrTypeOperator.SAFE_CAST -> TODO("${ir2string(value)}")