[Wasm] Support packed integer array elements

This commit is contained in:
Svyatoslav Kuzmich
2020-12-12 16:26:21 +03:00
parent 51e8d782b0
commit d37271bf35
4 changed files with 61 additions and 39 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.wasm.ir.WasmOp
import templates.COMMON_AUTOGENERATED_WARNING
import templates.COPYRIGHT_NOTICE
import templates.PrimitiveType
import templates.isUnsigned
import java.io.File
import java.io.FileWriter
@@ -56,20 +57,41 @@ fun generateWasmArrays(targetDir: File) {
writer.generateStandardWasmInternalHeader()
writer.appendLine(wasmArrayForType("Any", true))
writer.appendLine(wasmArrayForType("Boolean", false))
PrimitiveType.numericPrimitives.sortedBy { it.capacity }.forEach { primitive ->
writer.appendLine(wasmArrayForType(primitive.name, false))
PrimitiveType.descendingByDomainCapacity.reversed().forEach { primitive ->
val isPacked = primitive in setOf(
PrimitiveType.Byte,
PrimitiveType.Short,
PrimitiveType.Char,
)
val isUnsigned = primitive.isUnsigned() || primitive == PrimitiveType.Char
writer.appendLine(
wasmArrayForType(
primitive.name,
false, isPacked, isUnsigned
)
)
}
}
}
fun wasmArrayForType(klass: String, isNullable: Boolean): String {
fun wasmArrayForType(
klass: String,
isNullable: Boolean,
isPacked: Boolean = false,
isUnsigned: Boolean = false,
): String {
val type = klass + if (isNullable) "?" else ""
val name = "Wasm${klass}Array"
val getSuffix = when {
isPacked && isUnsigned -> "_U"
isPacked && !isUnsigned -> "_S"
else -> ""
}
return """
@WasmArrayOf($klass::class, isNullable = $isNullable)
internal class $name(size: Int) {
@WasmOp(WasmOp.ARRAY_GET)
@WasmOp(WasmOp.ARRAY_GET${getSuffix})
fun get(index: Int): $type =
implementedAsIntrinsic