translator: fix bit operators with different arguments

This commit is contained in:
Alexey Stepanov
2016-08-10 11:51:58 +03:00
parent 6fc0ed5f61
commit a8b2129bb9
6 changed files with 64 additions and 3 deletions
@@ -594,9 +594,34 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
"or" -> firstNativeOp.type!!.operatorOr(firstNativeOp, secondNativeOp)
"xor" -> firstNativeOp.type!!.operatorXor(firstNativeOp, secondNativeOp)
"and" -> firstNativeOp.type!!.operatorAnd(firstNativeOp, secondNativeOp)
"shl" -> firstNativeOp.type!!.operatorShl(firstNativeOp, secondNativeOp)
"shr" -> firstNativeOp.type!!.operatorShr(firstNativeOp, secondNativeOp)
"ushr" -> firstNativeOp.type!!.operatorUshr(firstNativeOp, secondNativeOp)
"shl" -> {
var secondNativeOpWithRequiredType = secondNativeOp
if (firstNativeOp.type != secondNativeOp.type) {
val convertedExpression = firstNativeOp.type!!.convertFrom(secondNativeOp)
secondNativeOpWithRequiredType = codeBuilder.getNewVariable(convertedExpression.variableType)
codeBuilder.addAssignment(secondNativeOpWithRequiredType, convertedExpression)
}
firstNativeOp.type!!.operatorShl(firstNativeOp, secondNativeOpWithRequiredType)
}
"shr" -> {
var secondNativeOpWithRequiredType = secondNativeOp
if (firstNativeOp.type != secondNativeOp.type) {
val convertedExpression = firstNativeOp.type!!.convertFrom(secondNativeOp)
secondNativeOpWithRequiredType = codeBuilder.getNewVariable(convertedExpression.variableType)
codeBuilder.addAssignment(secondNativeOpWithRequiredType, convertedExpression)
}
firstNativeOp.type!!.operatorShr(firstNativeOp, secondNativeOpWithRequiredType)
}
"ushr" -> {
var secondNativeOpWithRequiredType = secondNativeOp
if (firstNativeOp.type != secondNativeOp.type) {
val convertedExpression = firstNativeOp.type!!.convertFrom(secondNativeOp)
secondNativeOpWithRequiredType = codeBuilder.getNewVariable(convertedExpression.variableType)
codeBuilder.addAssignment(secondNativeOpWithRequiredType, convertedExpression)
}
firstNativeOp.type!!.operatorUshr(firstNativeOp, secondNativeOpWithRequiredType)
}
"+=" -> {
val llvmExpression = firstNativeOp.type!!.operatorPlus(firstNativeOp, secondNativeOp)
val resultOp = codeBuilder.getNewVariable(llvmExpression.variableType)
@@ -1,7 +1,9 @@
package org.kotlinnative.translator.llvm.types
import org.kotlinnative.translator.exceptions.UnimplementedException
import org.kotlinnative.translator.llvm.LLVMExpression
import org.kotlinnative.translator.llvm.LLVMSingleValue
import org.kotlinnative.translator.llvm.LLVMVariable
class LLVMIntType() : LLVMType() {
@@ -60,6 +62,14 @@ class LLVMIntType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i32 $firstOp, $secondOp")
override fun convertFrom(source: LLVMSingleValue): LLVMExpression = when (source.type!!) {
is LLVMBooleanType,
is LLVMByteType,
is LLVMCharType,
is LLVMShortType -> LLVMExpression(LLVMBooleanType(), " sext ${source.type} $source to i32")
else -> throw UnimplementedException()
}
override fun mangle() = "Int"
override val align = 4
@@ -1,5 +1,6 @@
package org.kotlinnative.translator.llvm.types
import org.kotlinnative.translator.exceptions.UnimplementedException
import org.kotlinnative.translator.llvm.LLVMExpression
import org.kotlinnative.translator.llvm.LLVMSingleValue
@@ -55,6 +56,15 @@ class LLVMLongType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i64 $firstOp, $secondOp")
override fun convertFrom(source: LLVMSingleValue): LLVMExpression = when (source.type!!) {
is LLVMBooleanType,
is LLVMByteType,
is LLVMCharType,
is LLVMShortType,
is LLVMIntType -> LLVMExpression(LLVMBooleanType(), " sext ${source.type} $source to i64")
else -> throw UnimplementedException()
}
override val align = 4
override var size: Int = 8
override val defaultValue = "0"
@@ -3,6 +3,7 @@ package org.kotlinnative.translator.llvm.types
import org.kotlinnative.translator.exceptions.UnimplementedException
import org.kotlinnative.translator.llvm.LLVMExpression
import org.kotlinnative.translator.llvm.LLVMSingleValue
import org.kotlinnative.translator.llvm.LLVMVariable
abstract class LLVMType() : Cloneable {
@@ -27,6 +28,7 @@ abstract class LLVMType() : Cloneable {
fun makeClone() = clone()
open fun convertFrom(source: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
abstract fun mangle(): String
abstract val align: Int
@@ -0,0 +1,3 @@
different_type_binary_operator_1_shl_Int_Long(10, 5) == 5120
different_type_binary_operator_1_shr_Int_Long(5, 5000) == 156
different_type_binary_operator_1_ushr_Int_Long(5, 5000) == 156
@@ -0,0 +1,11 @@
fun different_type_binary_operator_1_shr(x: Int, y: Long): Long {
return y shr x
}
fun different_type_binary_operator_1_shl(x: Int, y: Long): Long {
return y shl x
}
fun different_type_binary_operator_1_ushr(x: Int, y: Long): Long {
return y ushr x
}