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 cb58c7d7554..7ff3dd811a3 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 @@ -164,6 +164,7 @@ internal class CodeGenerator(override val context:Context) : ContextUtils { = LLVMPositionBuilderAtEnd(context.llvmBuilder, bbLabel) fun ret(value: LLVMOpaqueValue?) = LLVMBuildRet(context.llvmBuilder, value) + fun unreachable(): LLVMOpaqueValue? = LLVMBuildUnreachable(context.llvmBuilder) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt index c6c61b57756..f30a7e5c03f 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt @@ -30,6 +30,7 @@ internal class Context(val irModule: IrModuleFragment, val runtime: Runtime, val val lookupOpenMethodFunction = importRtFunction("LookupOpenMethod") val isInstanceFunction = importRtFunction("IsInstance") val checkInstanceFunction = importRtFunction("CheckInstance") + val throwExceptionFunction = importRtFunction("ThrowException") fun dispose() { LLVMDisposeBuilder(llvmBuilder) 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 4b767885513..c5516c87bc7 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 @@ -201,6 +201,13 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid //-------------------------------------------------------------------------// + override fun visitThrow(expression: IrThrow) { + logger.log("visitThrow : ${ir2string(expression)}") + evaluateExpression("", expression) + } + + //-------------------------------------------------------------------------// + override fun visitFunction(declaration: IrFunction) { logger.log("visitFunction : ${ir2string(declaration)}") codegen.function(declaration) @@ -328,6 +335,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid is IrBlock -> return evaluateBlock ( value) is IrExpressionBody -> return evaluateExpression (tmpVariableName, value.expression) is IrWhen -> return evaluateWhen (tmpVariableName, value) + is IrThrow -> return evaluateThrow (tmpVariableName, value) null -> return null else -> { TODO() @@ -337,6 +345,15 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid //-------------------------------------------------------------------------// + private fun evaluateThrow(tmpVariableName: String, expression: IrThrow): LLVMOpaqueValue? { + val objPointer = evaluateExpression(codegen.newVar(), expression.value) + val objHeaderPtr = codegen.bitcast(kObjHeaderPtr, objPointer!!, codegen.newVar()) + val args = listOf(objHeaderPtr) // Create arg list. + return codegen.call(context.throwExceptionFunction, args, "") + } + + //-------------------------------------------------------------------------// + private fun evaluateWhen(tmpVariableName:String, expression: IrWhen): LLVMOpaqueValue? { logger.log("evaluateWhen : ${ir2string(expression)}") var bbExit:LLVMOpaqueBasicBlock? = null // By default "when" does not have "exit".