Do..while loop implementation and first test

This commit is contained in:
Mikhail Glukhikh
2016-09-23 15:04:42 +03:00
committed by Dmitry Petrov
parent bcf2b410ba
commit e03e13af43
4 changed files with 86 additions and 0 deletions
@@ -143,6 +143,23 @@ class FunctionGenerator(val function: IrFunction) {
return condition
}
override fun visitDoWhileLoop(loop: IrDoWhileLoop, data: Boolean): IrElement? {
if (data) {
builder.add(loop)
}
val entry = MergeCfgElement(loop, "Do..while entry")
builder.jump(entry)
val body = loop.body
val condition = loop.condition
if (body?.process() !is IrReturn) {
condition.process(includeSelf = false)
builder.jump(condition)
builder.jump(entry)
builder.move(condition)
}
return condition
}
override fun visitElement(element: IrElement, data: Boolean): IrElement? {
TODO("not implemented")
}
+11
View File
@@ -0,0 +1,11 @@
fun digitCountInNumber(n: Int, m: Int): Int {
var count = 0
var number = n
do {
if (m == number % 10) {
count++
}
number /= 10
} while (number > 0)
return count
}
+52
View File
@@ -0,0 +1,52 @@
// FILE: /digitCount.kt
// FUN: digitCountInNumber
BB 0
CONTENT
1 FUN public fun digitCountInNumber(n: kotlin.Int, m: kotlin.Int): kotlin.Int
2 CONST Int type=kotlin.Int value='0'
3 VAR var count: kotlin.Int
4 GET_VAR 'value-parameter n: Int' type=kotlin.Int origin=null
5 VAR var number: kotlin.Int
6 DO_WHILE label=null origin=DO_WHILE_LOOP
OUTGOING -> BB 1
Do..while entry: DO_WHILE label=null origin=DO_WHILE_LOOP
BB 1
INCOMING <- BB 0, 4
Do..while entry: DO_WHILE label=null origin=DO_WHILE_LOOP
CONTENT
1 WHEN type=kotlin.Unit origin=null
OUTGOING -> BB 2
CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
BB 2
INCOMING <- BB 1
CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EQEQ
CONTENT
1 TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
OUTGOING -> BB 3
When exit: WHEN type=kotlin.Unit origin=null
BB 3
INCOMING <- BB 2
When exit: WHEN type=kotlin.Unit origin=null
CONTENT
1 SET_VAR 'number: Int' type=kotlin.Unit origin=DIVEQ
OUTGOING -> BB 4, 5
CALL 'GT0(Int): Boolean' type=kotlin.Boolean origin=GT
BB 4
INCOMING <- BB 3
CALL 'GT0(Int): Boolean' type=kotlin.Boolean origin=GT
CONTENT
OUTGOING -> BB 1
Do..while entry: DO_WHILE label=null origin=DO_WHILE_LOOP
BB 5
INCOMING <- BB 3
CALL 'GT0(Int): Boolean' type=kotlin.Boolean origin=GT
CONTENT
1 GET_VAR 'count: Int' type=kotlin.Int origin=null
2 RETURN type=kotlin.Nothing from='digitCountInNumber(Int, Int): Int'
OUTGOING -> NONE
Function exit: FUN public fun digitCountInNumber(n: kotlin.Int, m: kotlin.Int): kotlin.Int
// END FUN: digitCountInNumber
// END FILE: /digitCount.kt
@@ -79,6 +79,12 @@ public class IrCfgTestCaseGenerated extends AbstractIrCfgTestCase {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irCfg/loop"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("digitCount.kt")
public void testDigitCount() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/loop/digitCount.kt");
doTest(fileName);
}
@TestMetadata("factorial.kt")
public void testFactorial() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irCfg/loop/factorial.kt");