[Wasm] Move intrinsic generators to generators/wasm

Reason: avoid kotlin-stdlib-gen dependency on kotlinStdlib() via wasm.ir
This commit is contained in:
Svyatoslav Kuzmich
2020-12-15 17:39:24 +03:00
parent efeabac2c5
commit 602ed42b99
6 changed files with 58 additions and 28 deletions
@@ -1,13 +1,23 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.wasm.internal
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
// NOTE: THIS FILE IS AUTO-GENERATED by the generators/wasm/WasmIntrinsicGenerator.kt
//
@WasmArrayOf(Any::class, isNullable = true)
+13 -3
View File
@@ -1,13 +1,23 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.wasm.internal
//
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
// NOTE: THIS FILE IS AUTO-GENERATED by the generators/wasm/WasmIntrinsicGenerator.kt
//
@ExcludedFromCodegen
@@ -10,7 +10,6 @@ sourceSets {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$bootstrapKotlinVersion"
compile "org.jetbrains.kotlin:kotlin-reflect:$bootstrapKotlinVersion"
compile project(":wasm:wasm.ir")
}
compileKotlin {
@@ -5,8 +5,8 @@
package generators
import java.io.*
import templates.*
import java.io.File
import kotlin.system.exitProcess
/**
@@ -71,8 +71,6 @@ fun main(args: Array<String>) {
}
targetDir.resolve("_${source.name.capitalize()}$platformSuffix.kt")
}
targetBaseDirs[KotlinTarget.WASM]?.let { generateWasmBuiltIns(it) }
}
fun File.resolveExistingDir(subpath: String) = resolve(subpath).also { it.requireExistingDir() }
@@ -1,116 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package generators
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
fun generateWasmBuiltIns(targetDir: File) {
generateWasmOps(targetDir)
generateWasmArrays(targetDir)
}
fun FileWriter.generateStandardWasmInternalHeader() {
appendLine(COPYRIGHT_NOTICE)
appendLine("package kotlin.wasm.internal")
appendLine()
appendLine(COMMON_AUTOGENERATED_WARNING)
appendLine()
}
fun generateWasmOps(targetDir: File) {
FileWriter(targetDir.resolve("_WasmOp.kt")).use { writer ->
writer.generateStandardWasmInternalHeader()
writer.appendLine(
"""
@ExcludedFromCodegen
@Suppress("unused")
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
internal annotation class WasmOp(val name: String) {
companion object {
""".trimIndent()
)
WasmOp.values().forEach { op ->
writer.appendLine(" const val $op = \"$op\"")
}
writer.appendLine(
"""
}
}
""".trimIndent()
)
}
}
fun generateWasmArrays(targetDir: File) {
FileWriter(targetDir.resolve("_WasmArrays.kt")).use { writer ->
writer.generateStandardWasmInternalHeader()
writer.appendLine(wasmArrayForType("Any", true))
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,
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${getSuffix})
fun get(index: Int): $type =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_SET)
fun set(index: Int, value: $type): Unit =
implementedAsIntrinsic
@WasmOp(WasmOp.ARRAY_LEN)
fun len(): Int =
implementedAsIntrinsic
}
internal inline fun $name.fill(size: Int, init: (Int) -> $type) {
var i = 0
while (i < size) {
set(i, init(i))
i++
}
}
""".trimIndent()
}