Skip to content

Commit a3becd2

Browse files
committed
Fix for finally block for inline functions
For local return from inline function to outer scope just call super version of genReturn.
1 parent 1bf73fd commit a3becd2

File tree

1 file changed

+13
-34
lines changed
  • backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm

1 file changed

+13
-34
lines changed

backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,6 @@ internal interface CodeContext {
152152
*/
153153
fun genGetValue(descriptor: ValueDescriptor): LLVMValueRef
154154

155-
/**
156-
* Gets the exit block for the context owned by specified descriptor.
157-
*/
158-
fun getExit(descriptor: CallableDescriptor): LLVMBasicBlockRef
159-
160-
/**
161-
* Gets the result value for the context owned by specified descriptor.
162-
*/
163-
fun getResult(descriptor: CallableDescriptor): LLVMValueRef
164-
165155
/**
166156
* Returns owning function scope.
167157
*
@@ -208,10 +198,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
208198

209199
override fun genGetValue(descriptor: ValueDescriptor) = unsupported(descriptor)
210200

211-
override fun getExit(descriptor: CallableDescriptor): LLVMBasicBlockRef = unsupported(descriptor)
212-
213-
override fun getResult(descriptor: CallableDescriptor): LLVMValueRef = unsupported(descriptor)
214-
215201
override fun functionScope(): CodeContext = unsupported()
216202
}
217203

@@ -1395,35 +1381,31 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
13951381
var bbExit : LLVMBasicBlockRef? = null
13961382
var resultPhi : LLVMValueRef? = null
13971383

1398-
override fun getExit(descriptor: CallableDescriptor): LLVMBasicBlockRef {
1399-
if (descriptor != inlineBody.descriptor)
1400-
return super.getExit(descriptor)
1384+
private fun getExit(): LLVMBasicBlockRef {
14011385
if (bbExit == null) bbExit = codegen.basicBlock("inline_body_exit")
14021386
return bbExit!!
14031387
}
14041388

1405-
override fun getResult(descriptor: CallableDescriptor): LLVMValueRef {
1406-
if (descriptor != inlineBody.descriptor)
1407-
return super.getResult(descriptor)
1389+
private fun getResult(): LLVMValueRef {
14081390
if (resultPhi == null) {
14091391
val bbCurrent = codegen.currentBlock
1410-
codegen.positionAtEnd(getExit(descriptor))
1392+
codegen.positionAtEnd(getExit())
14111393
resultPhi = codegen.phi(codegen.getLLVMType(inlineBody.type))
14121394
codegen.positionAtEnd(bbCurrent)
14131395
}
14141396
return resultPhi!!
14151397
}
14161398

14171399
override fun genReturn(target: CallableDescriptor, value: LLVMValueRef?) {
1418-
if (target == codegen.functionDescriptor) { // It is "non local return".
1419-
super.genReturn(target, value) // Generate real "return".
1400+
if (target != inlineBody.descriptor) { // It is not our "local return".
1401+
super.genReturn(target, value)
14201402
return
14211403
}
1422-
// It is local return.
1423-
codegen.br(getExit(target)!!) // Generate branch on exit block.
1404+
// It is local return from current function.
1405+
codegen.br(getExit()) // Generate branch on exit block.
14241406

14251407
if (!KotlinBuiltIns.isUnit(inlineBody.type)) { // If function returns more then "unit"
1426-
codegen.assignPhis(getResult(target) to value!!) // Assign return value to result PHI node.
1408+
codegen.assignPhis(getResult() to value!!) // Assign return value to result PHI node.
14271409
}
14281410
}
14291411
}
@@ -1442,22 +1424,19 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
14421424
}
14431425
}
14441426

1445-
if (inlinedFunctionScope.bbExit != null) {
1427+
val bbExit = inlinedFunctionScope.bbExit
1428+
if (bbExit != null) {
14461429
if (!codegen.isAfterTerminator()) { // TODO should we solve this problem once and for all
14471430
if (inlinedFunctionScope.resultPhi != null) {
14481431
codegen.unreachable()
14491432
} else {
1450-
codegen.br(inlinedFunctionScope.bbExit!!)
1433+
codegen.br(bbExit)
14511434
}
14521435
}
1453-
codegen.positionAtEnd(inlinedFunctionScope.bbExit!!)
1436+
codegen.positionAtEnd(bbExit)
14541437
}
14551438

1456-
if (inlinedFunctionScope.resultPhi != null) {
1457-
return inlinedFunctionScope.resultPhi!!
1458-
} else {
1459-
return codegen.theUnitInstanceRef.llvm
1460-
}
1439+
return inlinedFunctionScope.resultPhi ?: codegen.theUnitInstanceRef.llvm
14611440
}
14621441

14631442
//-------------------------------------------------------------------------//

0 commit comments

Comments
 (0)