diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt index 7c0ab1152a4..30cbef46618 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2Native.kt @@ -189,6 +189,7 @@ class K2Native : CLICompiler() { put(FRIEND_MODULES, arguments.friendModules!!.split(File.pathSeparator).filterNot(String::isEmpty)) put(BITCODE_EMBEDDING_MODE, selectBitcodeEmbeddingMode(this, arguments, outputKind)) + put(DEBUG_INFO_VERSION, arguments.debugInfoFormatVersion.toInt()) } } } diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt index bc7daf8ec01..40c209f5e02 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.kt @@ -152,6 +152,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() { ) var friendModules: String? = null + @Argument(value = "-Xdebug-info-version", description = "generate debug info of given version (1, 2)") + var debugInfoFormatVersion: String = "1" /* command line parser doesn't accept kotlin.Int type */ + override fun configureAnalysisFlags(collector: MessageCollector): MutableMap, Any> = super.configureAnalysisFlags(collector).also { val useExperimental = it[AnalysisFlags.useExperimental] as List<*> diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt index ab898c93b63..a4ac173c08b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfigurationKeys.kt @@ -100,6 +100,9 @@ class KonanConfigKeys { = CompilerConfigurationKey.create("verify ir") val VERBOSE_PHASES: CompilerConfigurationKey> = CompilerConfigurationKey.create("verbose backend phases") + val DEBUG_INFO_VERSION: CompilerConfigurationKey + = CompilerConfigurationKey.create("debug info format version") + } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt index 539e93427a6..3b706b8331d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/DebugUtils.kt @@ -9,12 +9,13 @@ import kotlinx.cinterop.allocArrayOf import kotlinx.cinterop.memScoped import llvm.* import org.jetbrains.kotlin.backend.konan.Context -import org.jetbrains.kotlin.konan.KonanVersion +import org.jetbrains.kotlin.backend.konan.KonanConfig +import org.jetbrains.kotlin.backend.konan.KonanConfigKeys import org.jetbrains.kotlin.backend.konan.irasdescriptors.FunctionDescriptor import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.ir.SourceManager.FileEntry -import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.js.descriptorUtils.getJetTypeFqName +import org.jetbrains.kotlin.konan.KonanVersion import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeUtils @@ -23,18 +24,39 @@ import org.jetbrains.kotlin.types.TypeUtils internal object DWARF { val producer = "konanc ${KonanVersion.CURRENT} / kotlin-compiler: ${KotlinVersion.CURRENT}" /* TODO: from LLVM sources is unclear what runtimeVersion corresponds to term in terms of dwarf specification. */ - val runtimeVersion = 2 val dwarfVersionMetaDataNodeName = "Dwarf Version".mdString() val dwarfDebugInfoMetaDataNodeName = "Debug Info Version".mdString() - val dwarfVersion = 2 /* TODO: configurable? like gcc/clang -gdwarf-2 and so on. */ - val debugInfoVersion = 3 /* TODO: configurable? */ + const val debugInfoVersion = 3 /* TODO: configurable? */ /** * This is the value taken from [DIFlags.FlagFwdDecl], to mark type declaration as * forward one. */ - val flagsForwardDeclaration = 4 + const val flagsForwardDeclaration = 4 + + fun runtimeVersion(config: KonanConfig) = when (config.debugInfoVersion()) { + 2 -> 0 + 1 -> 2 /* legacy :/ */ + else -> TODO("unsupported debug info format version") + } + + /** + * Note: Kotlin language constant appears in DWARF v6, while modern linker fails to links DWARF other then [2;4], + * that why we emit version 4 actually. + */ + fun dwarfVersion(config : KonanConfig) = when (config.debugInfoVersion()) { + 1 -> 2 + 2 -> 4 /* likely the most of the future kotlin native debug info format versions will emit DWARF v4 */ + else -> TODO("unsupported debug info format version") + } + + fun language(config: KonanConfig) = when (config.debugInfoVersion()) { + 1 -> DwarfLanguage.DW_LANG_C89.value + else -> DwarfLanguage.DW_LANG_Kotlin.value + } } +fun KonanConfig.debugInfoVersion():Int = configuration[KonanConfigKeys.DEBUG_INFO_VERSION] ?: 1 + internal class DebugInfo internal constructor(override val context: Context):ContextUtils { val files = mutableMapOf() val subprograms = mutableMapOf() @@ -119,7 +141,7 @@ internal fun generateDebugInfoHeader(context: Context) { * !6 = !{!"Apple LLVM version 8.0.0 (clang-800.0.38)"} */ val llvmTwo = Int32(2).llvm - val dwarfVersion = node(llvmTwo, DWARF.dwarfVersionMetaDataNodeName, Int32(DWARF.dwarfVersion).llvm) + val dwarfVersion = node(llvmTwo, DWARF.dwarfVersionMetaDataNodeName, Int32(DWARF.dwarfVersion(context.config)).llvm) val nodeDebugInfoVersion = node(llvmTwo, DWARF.dwarfDebugInfoMetaDataNodeName, Int32(DWARF.debugInfoVersion).llvm) val llvmModuleFlags = "llvm.module.flags" LLVMAddNamedMetadataOperand(context.llvmModule, llvmModuleFlags, dwarfVersion) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Dwarf.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Dwarf.kt index 8a9d4a00043..15be2027d97 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Dwarf.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Dwarf.kt @@ -305,5 +305,5 @@ internal enum class DwarfLanguage(val value:Int) { DW_LANG_Mips_Assembler(0x8001), DW_LANG_GOOGLE_RenderScript(0x8e57), DW_LANG_BORLAND_Delphi(0xb000), - DW_LANG_Kotlin(0x0001) /* manually added, should be changed someday. */ + DW_LANG_Kotlin(0x0026) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 8998ecef8c7..b89786c1119 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -1907,13 +1907,13 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map