From 5088556f2877be89b1ffafa158256486ebd2c9c1 Mon Sep 17 00:00:00 2001 From: e5l Date: Wed, 10 Aug 2016 13:14:56 +0300 Subject: [PATCH] translator: fix while condition evaluation, minor fixes --- .../src/main/kotlin/CodedInputStream.kt | 23 +++++++------------ .../kotlinnative/translator/BlockCodegen.kt | 23 ++++++------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/proto/runtime/src/main/kotlin/CodedInputStream.kt b/proto/runtime/src/main/kotlin/CodedInputStream.kt index c4372875d98..f8c326aa51c 100644 --- a/proto/runtime/src/main/kotlin/CodedInputStream.kt +++ b/proto/runtime/src/main/kotlin/CodedInputStream.kt @@ -1,7 +1,3 @@ -import WireFormat.VARINT_INFO_BITS_COUNT -import WireFormat.VARINT_INFO_BITS_MASK -import WireFormat.VARINT_UTIL_BIT_MASK - /** * Created by Dmitry Savvinov on 7/6/16. * @@ -14,7 +10,6 @@ import WireFormat.VARINT_UTIL_BIT_MASK // TODO: refactor correctness checks into readTag class CodedInputStream(val buffer: ByteArray) { - var errorMessage: String = "" val inputStream: KotlinInputStream init { inputStream = KotlinInputStream(buffer) // TODO: Java's realization uses hand-written buffers. Why? @@ -66,7 +61,6 @@ class CodedInputStream(val buffer: ByteArray) { 0 -> false 1 -> true else -> { - errorMessage = "Expected boolean-encoding (1 or 0), got $readValue" false } } @@ -143,7 +137,6 @@ class CodedInputStream(val buffer: ByteArray) { } val tag = readInt32NoTag() if (tag == 0) { // if we somehow had read 0-tag, then message is corrupted - errorMessage = "Invalid tag 0" return 0 } @@ -158,16 +151,16 @@ class CodedInputStream(val buffer: ByteArray) { var done: Boolean = false var result: Int = 0 var step: Int = 0 - while (!done) { + while (done == false) { val byte: Int = inputStream.read().toInt() result = result or ( - (byte and VARINT_INFO_BITS_MASK) + (byte and WireFormat.VARINT_INFO_BITS_MASK) shl - (VARINT_INFO_BITS_COUNT * step) + (WireFormat.VARINT_INFO_BITS_COUNT * step) ) step++ - if ((byte and VARINT_UTIL_BIT_MASK) == 0) { + if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0) { done = true } } @@ -179,16 +172,16 @@ class CodedInputStream(val buffer: ByteArray) { var done: Boolean = false var result: Long = 0 var step: Int = 0 - while (!done) { + while (done == false) { val byte: Int = inputStream.read().toInt() result = result or ( - (byte and VARINT_INFO_BITS_MASK).toLong() + (byte and WireFormat.VARINT_INFO_BITS_MASK).toLong() shl - (VARINT_INFO_BITS_COUNT * step) + (WireFormat.VARINT_INFO_BITS_COUNT * step) ) step++ - if ((byte and VARINT_UTIL_BIT_MASK) == 0 /* || byte == -1 ???? */) { + if ((byte and WireFormat.VARINT_UTIL_BIT_MASK) == 0 /* || byte == -1 ???? */) { done = true } } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt index dc17150d779..ff73472e5a3 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/BlockCodegen.kt @@ -8,7 +8,6 @@ import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments -import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCallWithAssert import org.jetbrains.kotlin.resolve.calls.callUtil.getType @@ -73,10 +72,8 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va } private fun evaluateDoWhileExpression(element: PsiElement, scopeDepth: Int) { - val bodyExpression = element.getNextSiblingIgnoringWhitespaceAndComments() ?: return - val condition = bodyExpression.siblings(withItself = false).filter { it is KtContainerNode }.firstOrNull() ?: return - - executeWhileBlock(condition.firstChild as KtBinaryExpression, bodyExpression.firstChild, scopeDepth, checkConditionBeforeExecute = false) + val expr = element.context as KtDoWhileExpression + executeWhileBlock(expr.condition!!, expr.body!!, scopeDepth, checkConditionBeforeExecute = false) } fun evaluateExpression(expr: PsiElement?, scopeDepth: Int): LLVMSingleValue? { @@ -726,7 +723,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va return when (element.elementType) { KtTokens.RETURN_KEYWORD -> evaluateReturnInstruction(element, scopeDepth) KtTokens.IF_KEYWORD -> evaluateIfOperator(element.context as KtIfExpression, scopeDepth, isExpression = false) - KtTokens.WHILE_KEYWORD -> evaluateWhileOperator(element, scopeDepth) + KtTokens.WHILE_KEYWORD -> evaluateWhileOperator(element.context as KtWhileExpression, scopeDepth) else -> null } } @@ -794,23 +791,17 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va return resultVariable } - private fun evaluateWhileOperator(element: LeafPsiElement, scopeDepth: Int): LLVMVariable? { - var getBrackets = element.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - val condition = getBrackets.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - getBrackets = condition.getNextSiblingIgnoringWhitespaceAndComments() ?: return null - val bodyExpression = getBrackets.getNextSiblingIgnoringWhitespaceAndComments() ?: return null + private fun evaluateWhileOperator(expr: KtWhileExpression, scopeDepth: Int): LLVMVariable? = + executeWhileBlock(expr.condition!!, expr.body!!, scopeDepth, checkConditionBeforeExecute = true) - return executeWhileBlock(condition.firstChild as KtBinaryExpression, bodyExpression.firstChild, scopeDepth, checkConditionBeforeExecute = true) - } - - private fun executeWhileBlock(condition: KtBinaryExpression, bodyExpression: PsiElement, scopeDepth: Int, checkConditionBeforeExecute: Boolean): LLVMVariable? { + private fun executeWhileBlock(condition: KtExpression, bodyExpression: PsiElement, scopeDepth: Int, checkConditionBeforeExecute: Boolean): LLVMVariable? { val conditionLabel = codeBuilder.getNewLabel(prefix = "while") val bodyLabel = codeBuilder.getNewLabel(prefix = "while") val exitLabel = codeBuilder.getNewLabel(prefix = "while") codeBuilder.addUnconditionalJump(if (checkConditionBeforeExecute) conditionLabel else bodyLabel) codeBuilder.markWithLabel(conditionLabel) - val conditionResult = evaluateBinaryExpression(condition, scopeDepth + 1)!! + val conditionResult = evaluateExpression(condition, scopeDepth + 1)!! codeBuilder.addCondition(conditionResult, bodyLabel, exitLabel) evaluateCodeBlock(bodyExpression, bodyLabel, conditionLabel, scopeDepth + 1)