Fix wasm target
This commit is contained in:
committed by
Sergey Bogolepov
parent
ba2d427a12
commit
b8e31c2f6b
+5
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
|||||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||||
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
||||||
|
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
|
import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
|
||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
@@ -384,5 +385,9 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) {
|
|||||||
val isDynamicLibrary: Boolean by lazy {
|
val isDynamicLibrary: Boolean by lazy {
|
||||||
config.configuration.get(KonanConfigKeys.PRODUCE) == CompilerOutputKind.DYNAMIC
|
config.configuration.get(KonanConfigKeys.PRODUCE) == CompilerOutputKind.DYNAMIC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val isWasmTarget: Boolean by lazy {
|
||||||
|
config.targetManager.target == KonanTarget.WASM32
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -86,10 +86,11 @@ internal class LinkStage(val context: Context) {
|
|||||||
|
|
||||||
private fun bitcodeToWasm(bitcodeFiles: List<BitcodeFile>): String {
|
private fun bitcodeToWasm(bitcodeFiles: List<BitcodeFile>): String {
|
||||||
val combinedBc = temporary("combined", ".bc")
|
val combinedBc = temporary("combined", ".bc")
|
||||||
hostLlvmTool("llvm-link", bitcodeFiles + listOf("-o", combinedBc))
|
hostLlvmTool("llvm-link", bitcodeFiles + listOf("-o", combinedBc, "-internalize"))
|
||||||
|
val optimizedBc = temporary("opted", ".bc")
|
||||||
|
hostLlvmTool("opt", listOf(combinedBc, "-o", optimizedBc, "-O2"))
|
||||||
val combinedS = temporary("combined", ".s")
|
val combinedS = temporary("combined", ".s")
|
||||||
targetTool("llc", combinedBc, "-o", combinedS)
|
targetTool("llc", optimizedBc, "-o", combinedS)
|
||||||
|
|
||||||
val s2wasmFlags = (platform.configurables as WasmConfigurables).s2wasmFlags.toTypedArray()
|
val s2wasmFlags = (platform.configurables as WasmConfigurables).s2wasmFlags.toTypedArray()
|
||||||
val combinedWast = temporary( "combined", ".wast")
|
val combinedWast = temporary( "combined", ".wast")
|
||||||
|
|||||||
+3
@@ -270,3 +270,6 @@ internal val ClassDescriptor.objectInstanceFieldSymbolName: String
|
|||||||
|
|
||||||
internal val ClassDescriptor.typeInfoHasVtableAttached: Boolean
|
internal val ClassDescriptor.typeInfoHasVtableAttached: Boolean
|
||||||
get() = !this.isAbstract() && !this.isExternalObjCClass()
|
get() = !this.isAbstract() && !this.isExternalObjCClass()
|
||||||
|
|
||||||
|
internal fun FunctionDescriptor.isExportedForCppRuntime() =
|
||||||
|
annotations.hasAnnotation(exportForCppRuntimeAnnotation)
|
||||||
|
|||||||
+5
-1
@@ -629,6 +629,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
}.toMap()
|
}.toMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun shouldExportFunction(descriptor: FunctionDescriptor) =
|
||||||
|
descriptor.usedAnnotation
|
||||||
|
|| (context.isWasmTarget && descriptor.isExportedForCppRuntime())
|
||||||
|
|
||||||
override fun visitFunction(declaration: IrFunction) {
|
override fun visitFunction(declaration: IrFunction) {
|
||||||
context.log{"visitFunction : ${ir2string(declaration)}"}
|
context.log{"visitFunction : ${ir2string(declaration)}"}
|
||||||
val body = declaration.body
|
val body = declaration.body
|
||||||
@@ -656,7 +660,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (declaration.descriptor.usedAnnotation) {
|
if (shouldExportFunction(declaration.descriptor)) {
|
||||||
context.llvm.usedFunctions.add(codegen.llvmFunction(declaration.descriptor))
|
context.llvm.usedFunctions.add(codegen.llvmFunction(declaration.descriptor))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
@@ -89,6 +89,11 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils {
|
|||||||
val nameValue = annot.allValueArguments.values.single() as StringValue
|
val nameValue = annot.allValueArguments.values.single() as StringValue
|
||||||
// TODO: use LLVMAddAlias?
|
// TODO: use LLVMAddAlias?
|
||||||
val global = addGlobal(nameValue.value, pointerType(runtime.typeInfoType), isExported = true)
|
val global = addGlobal(nameValue.value, pointerType(runtime.typeInfoType), isExported = true)
|
||||||
|
|
||||||
|
if (context.isWasmTarget) {
|
||||||
|
context.llvm.usedGlobals += global
|
||||||
|
}
|
||||||
|
|
||||||
LLVMSetInitializer(global, typeInfoGlobal)
|
LLVMSetInitializer(global, typeInfoGlobal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "Common.h"
|
||||||
|
|
||||||
namespace konan {
|
namespace konan {
|
||||||
|
|
||||||
// Console operations.
|
// Console operations.
|
||||||
@@ -43,6 +45,32 @@ void* memmem(const void *big, size_t bigLen, const void *little, size_t littleLe
|
|||||||
int snprintf(char* buffer, size_t size, const char* format, ...);
|
int snprintf(char* buffer, size_t size, const char* format, ...);
|
||||||
size_t strnlen(const char* buffer, size_t maxSize);
|
size_t strnlen(const char* buffer, size_t maxSize);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#ifdef KONAN_WASM
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
double pow(double x, double y);
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
void *memcpy(void *dst, const void *src, size_t n);
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
void *memmove(void *dst, const void *src, size_t len);
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
int memcmp(const void *s1, const void *s2, size_t n);
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
void *memset(void *b, int c, size_t len);
|
||||||
|
|
||||||
|
RUNTIME_USED
|
||||||
|
size_t strlen(const char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// Memory operations.
|
// Memory operations.
|
||||||
void* calloc(size_t count, size_t size);
|
void* calloc(size_t count, size_t size);
|
||||||
void free(void* ptr);
|
void free(void* ptr);
|
||||||
|
|||||||
Reference in New Issue
Block a user