[Generator] Add otherKind parameter to conversion methods

This commit is contained in:
Ivan Kylchik
2023-07-27 15:15:49 +02:00
committed by Space Team
parent adce2c9a78
commit 6a9c50325e
4 changed files with 40 additions and 41 deletions
@@ -589,7 +589,7 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
methodName = "to$otherName"
returnType = otherName
}
}.modifyGeneratedConversions(thisKind)
}.modifyGeneratedConversions(thisKind, otherKind)
}
}
@@ -630,7 +630,7 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
internal open fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitwiseOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType, otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedEquals(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedToString(thisKind: PrimitiveType) {}
internal open fun ClassBuilder.generateAdditionalMethods(thisKind: PrimitiveType) {}
+17 -16
View File
@@ -277,7 +277,7 @@ abstract class CharGenerator(private val writer: PrintWriter) : BuiltInsGenerato
methodName = "to$otherName"
returnType = otherKind.capitalized
}
}.modifyGeneratedConversions()
}.modifyGeneratedConversions(otherKind)
}
}
@@ -328,7 +328,7 @@ abstract class CharGenerator(private val writer: PrintWriter) : BuiltInsGenerato
internal open fun MethodBuilder.modifyGeneratedDec() {}
internal open fun MethodBuilder.modifyGeneratedRangeTo() {}
internal open fun MethodBuilder.modifyGeneratedRangeUntil() {}
internal open fun MethodBuilder.modifyGeneratedConversions() {}
internal open fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedToString() {}
internal open fun MethodBuilder.modifyGeneratedEquals() {}
internal open fun MethodBuilder.modifyGeneratedHashCode() {}
@@ -402,10 +402,10 @@ class JsCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
"this until other".addAsSingleLineBody(bodyOnNewLine = false)
}
override fun MethodBuilder.modifyGeneratedConversions() {
val body = when (methodName) {
"toChar" -> "this"
"toInt" -> "value"
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
val body = when (otherKind) {
PrimitiveType.CHAR -> "this"
PrimitiveType.INT -> "value"
else -> "value.$methodName()"
}
body.addAsSingleLineBody(bodyOnNewLine = false)
@@ -526,11 +526,11 @@ class WasmCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
"this until other".addAsSingleLineBody(bodyOnNewLine = true)
}
override fun MethodBuilder.modifyGeneratedConversions() {
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
modifySignature { isInline = methodName != "toInt" }
val body = when (methodName) {
"toChar" -> "this"
"toInt" -> {
val body = when (otherKind) {
PrimitiveType.CHAR -> "this"
PrimitiveType.INT -> {
annotations += "WasmNoOpCast"
"implementedAsIntrinsic"
}
@@ -672,16 +672,17 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
"this until other".addAsSingleLineBody(bodyOnNewLine = true)
}
override fun MethodBuilder.modifyGeneratedConversions() {
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
modifySignature { isExternal = methodName != "toChar" }
when (methodName) {
"toByte" -> annotations += "TypedIntrinsic(IntrinsicType.INT_TRUNCATE)"
"toChar" -> {
when (otherKind) {
PrimitiveType.BYTE -> annotations += "TypedIntrinsic(IntrinsicType.INT_TRUNCATE)"
PrimitiveType.CHAR -> {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
}
"toShort", "toInt", "toLong" -> annotations += "TypedIntrinsic(IntrinsicType.ZERO_EXTEND)"
"toFloat", "toDouble" -> annotations += "TypedIntrinsic(IntrinsicType.UNSIGNED_TO_FLOAT)"
PrimitiveType.SHORT, PrimitiveType.INT, PrimitiveType.LONG -> annotations += "TypedIntrinsic(IntrinsicType.ZERO_EXTEND)"
PrimitiveType.FLOAT, PrimitiveType.DOUBLE -> annotations += "TypedIntrinsic(IntrinsicType.UNSIGNED_TO_FLOAT)"
else -> error("Unsupported `Char` conversion to $otherKind")
}
}
@@ -109,38 +109,37 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
setAsExternal(thisKind)
}
override fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType) {
val returnTypeAsPrimitive = PrimitiveType.valueOf(returnType.uppercase())
override fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType, otherKind: PrimitiveType) {
when {
returnTypeAsPrimitive == thisKind -> {
otherKind == thisKind -> {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
}
thisKind !in PrimitiveType.floatingPoint -> {
modifySignature { isExternal = true }
val intrinsicType = when {
returnTypeAsPrimitive in PrimitiveType.floatingPoint -> "SIGNED_TO_FLOAT"
returnTypeAsPrimitive.byteSize < thisKind.byteSize -> "INT_TRUNCATE"
returnTypeAsPrimitive.byteSize > thisKind.byteSize -> "SIGN_EXTEND"
otherKind in PrimitiveType.floatingPoint -> "SIGNED_TO_FLOAT"
otherKind.byteSize < thisKind.byteSize -> "INT_TRUNCATE"
otherKind.byteSize > thisKind.byteSize -> "SIGN_EXTEND"
else -> "ZERO_EXTEND"
}
annotations += "TypedIntrinsic(IntrinsicType.$intrinsicType)"
}
else -> {
if (returnTypeAsPrimitive in setOf(PrimitiveType.BYTE, PrimitiveType.SHORT, PrimitiveType.CHAR)) {
if (otherKind in setOf(PrimitiveType.BYTE, PrimitiveType.SHORT, PrimitiveType.CHAR)) {
"this.toInt().to${returnType}()".addAsSingleLineBody(bodyOnNewLine = false)
return
}
modifySignature { isExternal = true }
when {
returnTypeAsPrimitive in setOf(PrimitiveType.INT, PrimitiveType.LONG) -> {
otherKind in setOf(PrimitiveType.INT, PrimitiveType.LONG) -> {
annotations += "GCUnsafeCall(\"Kotlin_${thisKind.capitalized}_to${returnType}\")"
}
thisKind.byteSize > returnTypeAsPrimitive.byteSize -> {
thisKind.byteSize > otherKind.byteSize -> {
annotations += "TypedIntrinsic(IntrinsicType.FLOAT_TRUNCATE)"
}
thisKind.byteSize < returnTypeAsPrimitive.byteSize -> {
thisKind.byteSize < otherKind.byteSize -> {
annotations += "TypedIntrinsic(IntrinsicType.FLOAT_EXTEND)"
}
}
@@ -175,16 +175,15 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
implementAsIntrinsic(thisKind, methodName)
}
override fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType) {
val returnTypeAsPrimitive = PrimitiveType.valueOf(returnType.uppercase())
if (returnTypeAsPrimitive == thisKind) {
override fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType, otherKind: PrimitiveType) {
if (otherKind == thisKind) {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
return
}
when (thisKind) {
PrimitiveType.BYTE, PrimitiveType.SHORT -> when (returnTypeAsPrimitive) {
PrimitiveType.BYTE, PrimitiveType.SHORT -> when (otherKind) {
// byte to byte conversion impossible here due to earlier check on type equality
PrimitiveType.BYTE -> "this.toInt().toByte()".also { modifySignature { isInline = true } }
PrimitiveType.CHAR -> "reinterpretAsInt().reinterpretAsChar()"
@@ -193,33 +192,33 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
PrimitiveType.LONG -> "wasm_i64_extend_i32_s(this.toInt())"
PrimitiveType.FLOAT -> "wasm_f32_convert_i32_s(this.toInt())"
PrimitiveType.DOUBLE -> "wasm_f64_convert_i32_s(this.toInt())"
else -> throw IllegalArgumentException("Unsupported type $returnTypeAsPrimitive for generation conversion method from type $thisKind")
else -> throw IllegalArgumentException("Unsupported type $otherKind for generation conversion method from type $thisKind")
}
PrimitiveType.INT -> when (returnTypeAsPrimitive) {
PrimitiveType.INT -> when (otherKind) {
PrimitiveType.BYTE -> "((this shl 24) shr 24).reinterpretAsByte()"
PrimitiveType.CHAR -> "(this and 0xFFFF).reinterpretAsChar()"
PrimitiveType.SHORT -> "((this shl 16) shr 16).reinterpretAsShort()"
PrimitiveType.LONG -> "wasm_i64_extend_i32_s(this)"
PrimitiveType.FLOAT -> "wasm_f32_convert_i32_s(this)"
PrimitiveType.DOUBLE -> "wasm_f64_convert_i32_s(this)"
else -> throw IllegalArgumentException("Unsupported type $returnTypeAsPrimitive for generation conversion method from type $thisKind")
else -> throw IllegalArgumentException("Unsupported type $otherKind for generation conversion method from type $thisKind")
}
PrimitiveType.LONG -> when (returnTypeAsPrimitive) {
PrimitiveType.BYTE, PrimitiveType.CHAR, PrimitiveType.SHORT -> "this.toInt().to${returnTypeAsPrimitive.capitalized}()"
PrimitiveType.LONG -> when (otherKind) {
PrimitiveType.BYTE, PrimitiveType.CHAR, PrimitiveType.SHORT -> "this.toInt().to${otherKind.capitalized}()"
.also { modifySignature { isInline = true } }
PrimitiveType.INT -> "wasm_i32_wrap_i64(this)"
PrimitiveType.FLOAT -> "wasm_f32_convert_i64_s(this)"
PrimitiveType.DOUBLE -> "wasm_f64_convert_i64_s(this)"
else -> throw IllegalArgumentException("Unsupported type $returnTypeAsPrimitive for generation conversion method from type $thisKind")
else -> throw IllegalArgumentException("Unsupported type $otherKind for generation conversion method from type $thisKind")
}
in PrimitiveType.floatingPoint -> when (returnTypeAsPrimitive) {
PrimitiveType.BYTE, PrimitiveType.CHAR, PrimitiveType.SHORT -> "this.toInt().to${returnTypeAsPrimitive.capitalized}()"
in PrimitiveType.floatingPoint -> when (otherKind) {
PrimitiveType.BYTE, PrimitiveType.CHAR, PrimitiveType.SHORT -> "this.toInt().to${otherKind.capitalized}()"
.also { modifySignature { isInline = true } }
PrimitiveType.INT -> "wasm_i32_trunc_sat_${thisKind.prefixLowercase}_s(this)"
PrimitiveType.LONG -> "wasm_i64_trunc_sat_${thisKind.prefixLowercase}_s(this)"
PrimitiveType.FLOAT -> "wasm_f32_demote_f64(this)"
PrimitiveType.DOUBLE -> "wasm_f64_promote_f32(this)"
else -> throw IllegalArgumentException("Unsupported type $returnTypeAsPrimitive for generation conversion method from type $thisKind")
else -> throw IllegalArgumentException("Unsupported type $otherKind for generation conversion method from type $thisKind")
}
else -> throw IllegalArgumentException("Unsupported type $thisKind to generate conversion methods")
}.addAsSingleLineBody(bodyOnNewLine = false)