Fix init function name clash. (#1363)
This commit is contained in:
+23
-27
@@ -346,7 +346,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
|
|
||||||
appendLlvmUsed("llvm.used", context.llvm.usedFunctions + context.llvm.usedGlobals)
|
appendLlvmUsed("llvm.used", context.llvm.usedFunctions + context.llvm.usedGlobals)
|
||||||
appendLlvmUsed("llvm.compiler.used", context.llvm.compilerUsedGlobals)
|
appendLlvmUsed("llvm.compiler.used", context.llvm.compilerUsedGlobals)
|
||||||
appendStaticInitializers(context.llvm.staticInitializers)
|
appendStaticInitializers()
|
||||||
appendEntryPointSelector(findMainEntryPoint(context))
|
appendEntryPointSelector(findMainEntryPoint(context))
|
||||||
if (context.isDynamicLibrary) {
|
if (context.isDynamicLibrary) {
|
||||||
appendCAdapters()
|
appendCAdapters()
|
||||||
@@ -361,11 +361,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
val kNodeInitType = LLVMGetTypeByName(context.llvmModule, "struct.InitNode")!!
|
val kNodeInitType = LLVMGetTypeByName(context.llvmModule, "struct.InitNode")!!
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
private fun createInitBody(initName: String): LLVMValueRef {
|
private fun createInitBody(): LLVMValueRef {
|
||||||
val initFunction = LLVMAddFunction(context.llvmModule, initName, kInitFuncType)!! // create LLVM function
|
val initFunction = LLVMAddFunction(context.llvmModule, "", kInitFuncType)!!
|
||||||
LLVMSetLinkage(initFunction, LLVMLinkage.LLVMPrivateLinkage)
|
|
||||||
generateFunction(codegen, initFunction) {
|
generateFunction(codegen, initFunction) {
|
||||||
using(FunctionScope(initFunction, initName, it)) {
|
using(FunctionScope(initFunction, "init_body", it)) {
|
||||||
val bbInit = basicBlock("init", null)
|
val bbInit = basicBlock("init", null)
|
||||||
val bbDeinit = basicBlock("deinit", null)
|
val bbDeinit = basicBlock("deinit", null)
|
||||||
condBr(functionGenerationContext.icmpEq(LLVMGetParam(initFunction, 0)!!, kImmZero), bbDeinit, bbInit)
|
condBr(functionGenerationContext.icmpEq(LLVMGetParam(initFunction, 0)!!, kImmZero), bbDeinit, bbInit)
|
||||||
@@ -401,23 +400,25 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
// Creates static struct InitNode $nodeName = {$initName, NULL};
|
// Creates static struct InitNode $nodeName = {$initName, NULL};
|
||||||
|
|
||||||
private fun createInitNode(initFunction: LLVMValueRef, nodeName: String): LLVMValueRef {
|
private fun createInitNode(initFunction: LLVMValueRef): LLVMValueRef {
|
||||||
val nextInitNode = LLVMConstNull(pointerType(kNodeInitType)) // Set InitNode.next = NULL.
|
val nextInitNode = LLVMConstNull(pointerType(kNodeInitType))
|
||||||
val argList = cValuesOf(initFunction, nextInitNode) // Construct array of args.
|
val argList = cValuesOf(initFunction, nextInitNode)
|
||||||
val initNode = LLVMConstNamedStruct(kNodeInitType, argList, 2)!! // Create static object of class InitNode.
|
// Create static object of class InitNode.
|
||||||
return context.llvm.staticData.placeGlobal(nodeName, constPointer(initNode)).llvmGlobal // Put the object in global var with name "nodeName".
|
val initNode = LLVMConstNamedStruct(kNodeInitType, argList, 2)!!
|
||||||
|
// Create global variable with init record data.
|
||||||
|
return context.llvm.staticData.placeGlobal(
|
||||||
|
"init_node", constPointer(initNode), isExported = false).llvmGlobal
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
private fun createInitCtor(ctorName: String, initNodePtr: LLVMValueRef) {
|
private fun createInitCtor(initNodePtr: LLVMValueRef): LLVMValueRef {
|
||||||
val ctorFunction = LLVMAddFunction(context.llvmModule, ctorName, kVoidFuncType)!! // Create constructor function.
|
val ctorFunction = LLVMAddFunction(context.llvmModule, "", kVoidFuncType)!!
|
||||||
LLVMSetLinkage(ctorFunction, LLVMLinkage.LLVMExternalLinkage)
|
|
||||||
generateFunction(codegen, ctorFunction) {
|
generateFunction(codegen, ctorFunction) {
|
||||||
call(context.llvm.appendToInitalizersTail, listOf(initNodePtr)) // Add node to the tail of initializers list.
|
call(context.llvm.appendToInitalizersTail, listOf(initNodePtr))
|
||||||
ret(null)
|
ret(null)
|
||||||
}
|
}
|
||||||
context.llvm.staticInitializers.add(ctorFunction) // Push newly created constructor in staticInitializers list.
|
return ctorFunction
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
@@ -434,13 +435,8 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
return
|
return
|
||||||
|
|
||||||
// Create global initialization records.
|
// Create global initialization records.
|
||||||
val fileName = declaration.name.takeLastWhile { it != '/' }.dropLastWhile { it != '.' }.dropLast(1)
|
val initNode = createInitNode(createInitBody())
|
||||||
val initName = "${fileName}_init_${context.llvm.globalInitIndex}"
|
context.llvm.staticInitializers.add(createInitCtor(initNode))
|
||||||
val nodeName = "${fileName}_node_${context.llvm.globalInitIndex}"
|
|
||||||
val ctorName = "${fileName}_${context.llvm.globalInitIndex++}"
|
|
||||||
val initFunction = createInitBody(initName)
|
|
||||||
val initNode = createInitNode(initFunction, nodeName)
|
|
||||||
createInitCtor(ctorName, initNode)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2543,9 +2539,9 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
fun appendStaticInitializers(initializers: List<LLVMValueRef>) {
|
fun appendStaticInitializers() {
|
||||||
val ctorName = context.config.moduleId.moduleConstructorName
|
val ctorName = context.config.moduleId.moduleConstructorName
|
||||||
val ctorFunction = LLVMAddFunction(context.llvmModule, ctorName, kVoidFuncType)!! // Create constructor function.
|
val ctorFunction = LLVMAddFunction(context.llvmModule, ctorName, kVoidFuncType)!!
|
||||||
LLVMSetLinkage(ctorFunction, LLVMLinkage.LLVMExternalLinkage)
|
LLVMSetLinkage(ctorFunction, LLVMLinkage.LLVMExternalLinkage)
|
||||||
generateFunction(codegen, ctorFunction) {
|
generateFunction(codegen, ctorFunction) {
|
||||||
val initGuard = LLVMAddGlobal(context.llvmModule, int32Type, "Konan_init_guard")
|
val initGuard = LLVMAddGlobal(context.llvmModule, int32Type, "Konan_init_guard")
|
||||||
@@ -2573,7 +2569,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: shall we put that into the try block?
|
// TODO: shall we put that into the try block?
|
||||||
initializers.forEach {
|
context.llvm.staticInitializers.forEach {
|
||||||
call(it, emptyList(), Lifetime.IRRELEVANT,
|
call(it, emptyList(), Lifetime.IRRELEVANT,
|
||||||
exceptionHandler = ExceptionHandler.Caller, verbatim = true)
|
exceptionHandler = ExceptionHandler.Caller, verbatim = true)
|
||||||
}
|
}
|
||||||
@@ -2582,12 +2578,12 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (context.config.produce.isNativeBinary) {
|
if (context.config.produce.isNativeBinary) {
|
||||||
// Append initializers of global variables in "llvm.global_ctors" array
|
// Append initializers of global variables in "llvm.global_ctors" array.
|
||||||
val globalCtors = context.llvm.staticData.placeGlobalArray("llvm.global_ctors", kCtorType,
|
val globalCtors = context.llvm.staticData.placeGlobalArray("llvm.global_ctors", kCtorType,
|
||||||
listOf(createGlobalCtor(ctorFunction)))
|
listOf(createGlobalCtor(ctorFunction)))
|
||||||
LLVMSetLinkage(globalCtors.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage)
|
LLVMSetLinkage(globalCtors.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage)
|
||||||
if (context.config.produce == CompilerOutputKind.PROGRAM) {
|
if (context.config.produce == CompilerOutputKind.PROGRAM) {
|
||||||
// Provide an optional handle for calling .ctors, if standard constrcutors mechanism
|
// Provide an optional handle for calling .ctors, if standard constructors mechanism
|
||||||
// is not available on the platform (i.e. WASM, embedded).
|
// is not available on the platform (i.e. WASM, embedded).
|
||||||
val globalCtorFunction = LLVMAddFunction(context.llvmModule, "_Konan_constructors", kVoidFuncType)!!
|
val globalCtorFunction = LLVMAddFunction(context.llvmModule, "_Konan_constructors", kVoidFuncType)!!
|
||||||
LLVMSetLinkage(globalCtorFunction, LLVMLinkage.LLVMExternalLinkage)
|
LLVMSetLinkage(globalCtorFunction, LLVMLinkage.LLVMExternalLinkage)
|
||||||
|
|||||||
Reference in New Issue
Block a user