diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt index 3ace873f3c5..0bb91eca133 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt @@ -33,6 +33,13 @@ class FunctionCodegen(override val state: TranslationState, LLVMInstanceOfStandardType(it.name.toString(), it.type) }) + if (isExtensionDeclaration) { + val receiverType = descriptor.extensionReceiverParameter!!.type + val translatorType = LLVMMapStandardType(receiverType) + functionNamePrefix += translatorType.toString() + "." + } + + returnType = LLVMInstanceOfStandardType("instance", descriptor.returnType!!) external = isExternal() name = "${function.fqName}${if (args.size > 0 && !external) "_${args.joinToString(separator = "_", transform = { it.type.mangle() })}" else ""}" @@ -98,7 +105,6 @@ class FunctionCodegen(override val state: TranslationState, val classVal = LLVMVariable("classvariable.this", translatorType, pointer = 0) variableManager.addVariable("this", classVal, 0) actualArgs.add(classVal) - functionNamePrefix += translatorType.toString() + "." } if (this_type != null) { diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/ProjectTranslator.kt b/translator/src/main/kotlin/org/kotlinnative/translator/ProjectTranslator.kt index 97bc6236e1d..52cb398aa5f 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/ProjectTranslator.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/ProjectTranslator.kt @@ -18,9 +18,9 @@ class ProjectTranslator(val files: List, val state: TranslationState) { is KtNamedFunction -> { val function = FunctionCodegen(state, VariableManager(state.globalVariableCollection), declaration, codeBuilder) if (function.external) { - state.externalFunctions.put(function.name, function) + state.externalFunctions.put(function.fullName, function) } else { - state.functions.put(function.name, function) + state.functions.put(function.fullName, function) } } is KtClass -> { diff --git a/translator/src/main/resources/kotlib/kotlin/ByteArray.kt b/translator/src/main/resources/kotlib/kotlin/ByteArray.kt index fddd12f76a1..a62fc3b777c 100644 --- a/translator/src/main/resources/kotlib/kotlin/ByteArray.kt +++ b/translator/src/main/resources/kotlib/kotlin/ByteArray.kt @@ -38,3 +38,4 @@ class ByteArray(var size: Int) { } } + diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesByte.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesByte.kt new file mode 100644 index 00000000000..ae0899e1f60 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesByte.kt @@ -0,0 +1,35 @@ +external fun kotlinclib_byteToChar(value: Byte): Char +external fun kotlinclib_byteToShort(value: Byte): Short +external fun kotlinclib_byteToInt(value: Byte): Int +external fun kotlinclib_byteToLong(value: Byte): Long +external fun kotlinclib_byteToFloat(value: Byte): Float +external fun kotlinclib_byteToDouble(value: Byte): Double + + +fun Byte.toByte(): Byte { + return this +} + +fun Byte.toInt(): Int { + return kotlinclib_byteToInt(this) +} + +fun Byte.toChar(): Char { + return kotlinclib_byteToChar(this) +} + +fun Byte.toShort(): Short { + return kotlinclib_byteToShort(this) +} + +fun Byte.toLong(): Long { + return kotlinclib_byteToLong(this) +} + +fun Byte.toFloat(): Float { + return kotlinclib_byteToFloat(this) +} + +fun Byte.toDouble(): Double { + return kotlinclib_byteToDouble(this) +} \ No newline at end of file diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesChar.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesChar.kt new file mode 100644 index 00000000000..c0ea89d9c24 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesChar.kt @@ -0,0 +1,35 @@ +external fun kotlinclib_charToByte(value: Char): Byte +external fun kotlinclib_charToShort(value: Char): Short +external fun kotlinclib_charToInt(value: Char): Int +external fun kotlinclib_charToLong(value: Char): Long +external fun kotlinclib_charToFloat(value: Char): Float +external fun kotlinclib_charToDouble(value: Char): Double + + +fun Char.toByte(): Byte { + return kotlinclib_charToByte(this) +} + +fun Char.toInt(): Int { + return kotlinclib_charToInt(this) +} + +fun Char.toChar(): Char { + return this +} + +fun Char.toShort(): Short { + return kotlinclib_charToShort(this) +} + +fun Char.toLong(): Long { + return kotlinclib_charToLong(this) +} + +fun Char.toFloat(): Float { + return kotlinclib_charToFloat(this) +} + +fun Char.toDouble(): Double { + return kotlinclib_charToDouble(this) +} diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesDouble.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesDouble.kt new file mode 100644 index 00000000000..79dc9466a03 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesDouble.kt @@ -0,0 +1,34 @@ +external fun kotlinclib_doubleToByte(value: Double): Byte +external fun kotlinclib_doubleToChar(value: Double): Char +external fun kotlinclib_doubleToShort(value: Double): Short +external fun kotlinclib_doubleToInt(value: Double): Int +external fun kotlinclib_doubleToLong(value: Double): Long +external fun kotlinclib_doubleToFloat(value: Double): Float + +fun Double.toByte(): Byte { + return kotlinclib_doubleToByte(this) +} + +fun Double.toChar(): Char { + return kotlinclib_doubleToChar(this) +} + +fun Double.toShort(): Short { + return kotlinclib_doubleToShort(this) +} + +fun Double.toInt(): Int { + return kotlinclib_doubleToInt(this) +} + +fun Double.toLong(): Long { + return kotlinclib_doubleToLong(this) +} + +fun Double.toFloat(): Float { + return kotlinclib_doubleToFloat(this) +} + +fun Double.toDouble(): Double { + return this +} diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesFloat.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesFloat.kt new file mode 100644 index 00000000000..7ee9b8493dd --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesFloat.kt @@ -0,0 +1,34 @@ +external fun kotlinclib_floatToByte(value: Float): Byte +external fun kotlinclib_floatToChar(value: Float): Char +external fun kotlinclib_floatToShort(value: Float): Short +external fun kotlinclib_floatToInt(value: Float): Int +external fun kotlinclib_floatToLong(value: Float): Long +external fun kotlinclib_floatToDouble(value: Float): Double + +fun Float.toByte(): Byte { + return kotlinclib_floatToByte(this) +} + +fun Float.toChar(): Char { + return kotlinclib_floatToChar(this) +} + +fun Float.toShort(): Short { + return kotlinclib_floatToShort(this) +} + +fun Float.toInt(): Int { + return kotlinclib_floatToInt(this) +} + +fun Float.toLong(): Long { + return kotlinclib_floatToLong(this) +} + +fun Float.toFloat(): Float { + return this +} + +fun Float.toDouble(): Double { + return kotlinclib_floatToDouble(this) +} diff --git a/translator/src/main/resources/kotlib/kotlin/Primitives.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesInt.kt similarity index 100% rename from translator/src/main/resources/kotlib/kotlin/Primitives.kt rename to translator/src/main/resources/kotlib/kotlin/PrimitivesInt.kt diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesLong.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesLong.kt new file mode 100644 index 00000000000..566983cd879 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesLong.kt @@ -0,0 +1,34 @@ +external fun kotlinclib_longToByte(value: Long): Byte +external fun kotlinclib_longToChar(value: Long): Char +external fun kotlinclib_longToShort(value: Long): Short +external fun kotlinclib_longToInt(value: Long): Int +external fun kotlinclib_longToFloat(value: Long): Float +external fun kotlinclib_longToDouble(value: Long): Double + +fun Long.toByte(): Byte { + return kotlinclib_longToByte(this) +} + +fun Long.toLong(): Long { + return this +} + +fun Long.toChar(): Char { + return kotlinclib_longToChar(this) +} + +fun Long.toShort(): Short { + return kotlinclib_longToShort(this) +} + +fun Long.toInt(): Int { + return kotlinclib_longToInt(this) +} + +fun Long.toFloat(): Float { + return kotlinclib_longToFloat(this) +} + +fun Long.toDouble(): Double { + return kotlinclib_longToDouble(this) +} diff --git a/translator/src/main/resources/kotlib/kotlin/PrimitivesShort.kt b/translator/src/main/resources/kotlib/kotlin/PrimitivesShort.kt new file mode 100644 index 00000000000..07aeed05522 --- /dev/null +++ b/translator/src/main/resources/kotlib/kotlin/PrimitivesShort.kt @@ -0,0 +1,35 @@ +external fun kotlinclib_shortToByte(value: Short): Byte +external fun kotlinclib_shortToChar(value: Short): Char +external fun kotlinclib_shortToInt(value: Short): Int +external fun kotlinclib_shortToLong(value: Short): Long +external fun kotlinclib_shortToFloat(value: Short): Float +external fun kotlinclib_shortToDouble(value: Short): Double + + +fun Short.toByte(): Byte { + return kotlinclib_shortToByte(this) +} + +fun Short.toInt(): Int { + return kotlinclib_shortToInt(this) +} + +fun Short.toChar(): Char { + return kotlinclib_shortToChar(this) +} + +fun Short.toShort(): Short { + return this +} + +fun Short.toLong(): Long { + return kotlinclib_shortToLong(this) +} + +fun Short.toFloat(): Float { + return kotlinclib_shortToFloat(this) +} + +fun Short.toDouble(): Double { + return kotlinclib_shortToDouble(this) +} diff --git a/translator/src/main/resources/kotlib/linked/primitives_c.ll b/translator/src/main/resources/kotlib/linked/primitives_c.ll index 5262af7bc5c..d0ecf2018a1 100644 --- a/translator/src/main/resources/kotlib/linked/primitives_c.ll +++ b/translator/src/main/resources/kotlib/linked/primitives_c.ll @@ -56,4 +56,326 @@ define double @kotlinclib_intToDouble(i32 %value) #0 { ret double %3 } -attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_byteToChar(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + ret i8 %2 +} + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_byteToShort(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i16 + ret i16 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_byteToInt(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define i64 @kotlinclib_byteToLong(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i64 + ret i64 %3 +} + +; Function Attrs: nounwind uwtable +define float @kotlinclib_byteToFloat(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sitofp i8 %2 to float + ret float %3 +} + +; Function Attrs: nounwind uwtable +define double @kotlinclib_byteToDouble(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sitofp i8 %2 to double + ret double %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_charToByte(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + ret i8 %2 +} + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_charToShort(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i16 + ret i16 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_charToInt(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define i64 @kotlinclib_charToLong(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sext i8 %2 to i64 + ret i64 %3 +} + +; Function Attrs: nounwind uwtable +define float @kotlinclib_charToFloat(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sitofp i8 %2 to float + ret float %3 +} + +; Function Attrs: nounwind uwtable +define double @kotlinclib_charToDouble(i8 signext %value) #0 { + %1 = alloca i8, align 1 + store i8 %value, i8* %1, align 1 + %2 = load i8* %1, align 1 + %3 = sitofp i8 %2 to double + ret double %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_shortToByte(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = trunc i16 %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_shortToChar(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = trunc i16 %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_shortToInt(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = sext i16 %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define i64 @kotlinclib_shortToLong(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = sext i16 %2 to i64 + ret i64 %3 +} + +; Function Attrs: nounwind uwtable +define float @kotlinclib_shortToFloat(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = sitofp i16 %2 to float + ret float %3 +} + +; Function Attrs: nounwind uwtable +define double @kotlinclib_shortToDouble(i16 signext %value) #0 { + %1 = alloca i16, align 2 + store i16 %value, i16* %1, align 2 + %2 = load i16* %1, align 2 + %3 = sitofp i16 %2 to double + ret double %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_longToByte(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = trunc i64 %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_longToChar(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = trunc i64 %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_longToShort(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = trunc i64 %2 to i16 + ret i16 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_longToInt(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = trunc i64 %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define float @kotlinclib_longToFloat(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = sitofp i64 %2 to float + ret float %3 +} + +; Function Attrs: nounwind uwtable +define double @kotlinclib_longToDouble(i64 %value) #0 { + %1 = alloca i64, align 8 + store i64 %value, i64* %1, align 8 + %2 = load i64* %1, align 8 + %3 = sitofp i64 %2 to double + ret double %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_floatToByte(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fptosi float %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_floatToChar(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fptosi float %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_floatToShort(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fptosi float %2 to i16 + ret i16 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_floatToInt(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fptosi float %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define i64 @kotlinclib_floatToLong(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fptosi float %2 to i64 + ret i64 %3 +} + +; Function Attrs: nounwind uwtable +define double @kotlinclib_floatToDouble(float %value) #0 { + %1 = alloca float, align 4 + store float %value, float* %1, align 4 + %2 = load float* %1, align 4 + %3 = fpext float %2 to double + ret double %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_doubleToByte(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptosi double %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i8 @kotlinclib_doubleToChar(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptosi double %2 to i8 + ret i8 %3 +} + +; Function Attrs: nounwind uwtable +define signext i16 @kotlinclib_doubleToShort(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptosi double %2 to i16 + ret i16 %3 +} + +; Function Attrs: nounwind uwtable +define i32 @kotlinclib_doubleToInt(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptosi double %2 to i32 + ret i32 %3 +} + +; Function Attrs: nounwind uwtable +define i64 @kotlinclib_doubleToLong(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptosi double %2 to i64 + ret i64 %3 +} + +; Function Attrs: nounwind uwtable +define float @kotlinclib_doubleToFloat(double %value) #0 { + %1 = alloca double, align 8 + store double %value, double* %1, align 8 + %2 = load double* %1, align 8 + %3 = fptrunc double %2 to float + ret float %3 +} + +attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } \ No newline at end of file diff --git a/translator/src/test/kotlin/tests/input/primitives_1.txt b/translator/src/test/kotlin/tests/input/primitives_1.txt index ad66eee290b..3da9e622a39 100644 --- a/translator/src/test/kotlin/tests/input/primitives_1.txt +++ b/translator/src/test/kotlin/tests/input/primitives_1.txt @@ -2,3 +2,5 @@ testCastToInt_Int(32) == 0 testCastToInt_Int(34) == 1 testCastToByte_Byte(32) == 0 testCastToByte_Byte(34) == 1 +testCastToShort_Short(32) == 0 +testCastToShort_Short(34) == 1 diff --git a/translator/src/test/kotlin/tests/kotlin/bytearray_1.kt b/translator/src/test/kotlin/tests/kotlin/bytearray_1.kt index 941707bae88..b6b29253135 100644 --- a/translator/src/test/kotlin/tests/kotlin/bytearray_1.kt +++ b/translator/src/test/kotlin/tests/kotlin/bytearray_1.kt @@ -1,5 +1,6 @@ fun bytearray_1(x: Byte): Byte { val z = ByteArray(10) z.set(1, x) - return z.get(1) + val r = z.clone() + return r.get(1) } \ No newline at end of file diff --git a/translator/src/test/kotlin/tests/kotlin/primitives_1.kt b/translator/src/test/kotlin/tests/kotlin/primitives_1.kt index a4551428c53..ca3ea2ee390 100644 --- a/translator/src/test/kotlin/tests/kotlin/primitives_1.kt +++ b/translator/src/test/kotlin/tests/kotlin/primitives_1.kt @@ -5,3 +5,7 @@ fun testCastToByte(X : Byte): Boolean{ fun testCastToInt(X : Int): Boolean{ return 34.toInt() == X } + +fun testCastToShort(X : Short): Boolean{ + return 34.toShort() == X +} \ No newline at end of file