Generate trap before each unreachable instruction in debug mode

This commit is contained in:
Svyatoslav Scherbina
2019-04-02 09:41:54 +03:00
committed by SvyatoslavScherbina
parent 9cc6000237
commit 68efc823f1
3 changed files with 26 additions and 7 deletions
@@ -424,6 +424,9 @@ internal class FunctionGenerationContext(val function: LLVMValueRef,
call(context.llvm.allocArrayFunction, listOf(typeInfo, count), lifetime, exceptionHandler)
fun unreachable(): LLVMValueRef? {
if (context.config.debug) {
call(context.llvm.llvmTrap, emptyList())
}
val res = LLVMBuildUnreachable(builder)
currentPositionHolder.setAfterTerminator()
return res
@@ -296,6 +296,16 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
return LLVMAddFunction(llvmModule, "llvm.memcpy.p0i8.p0i8.i32", functionType)!!
}
private fun llvmIntrinsic(name: String, type: LLVMTypeRef, vararg attributes: String): LLVMValueRef {
val result = LLVMAddFunction(llvmModule, name, type)!!
attributes.forEach {
val kindId = getLlvmAttributeKindId(it)
val attribute = LLVMCreateEnumAttribute(LLVMGetTypeContext(type), kindId, 0)!!
LLVMAddAttributeAtIndex(result, LLVMAttributeFunctionIndex, attribute)
}
return result
}
internal fun externalFunction(
name: String,
type: LLVMTypeRef,
@@ -474,6 +484,12 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
val memsetFunction = importMemset()
//val memcpyFunction = importMemcpy()
val llvmTrap = llvmIntrinsic(
"llvm.trap",
functionType(voidType, false),
"cold", "noreturn", "nounwind"
)
val usedFunctions = mutableListOf<LLVMValueRef>()
val usedGlobals = mutableListOf<LLVMValueRef>()
val compilerUsedGlobals = mutableListOf<LLVMValueRef>()
@@ -277,15 +277,15 @@ fun parseBitcodeFile(path: String): LLVMModuleRef = memScoped {
}
private val nounwindAttrKindId by lazy {
getAttributeKindId("nounwind")
getLlvmAttributeKindId("nounwind")
}
private val noreturnAttrKindId by lazy {
getAttributeKindId("noreturn")
getLlvmAttributeKindId("noreturn")
}
private val signextAttrKindId by lazy {
getAttributeKindId("signext")
getLlvmAttributeKindId("signext")
}
@@ -294,12 +294,12 @@ fun isFunctionNoUnwind(function: LLVMValueRef): Boolean {
return attribute != null
}
private fun getAttributeKindId(attributeName: String): Int {
val nounwindAttrKindId = LLVMGetEnumAttributeKindForName(attributeName, attributeName.length.signExtend())
if (nounwindAttrKindId == 0) {
internal fun getLlvmAttributeKindId(attributeName: String): Int {
val attrKindId = LLVMGetEnumAttributeKindForName(attributeName, attributeName.length.signExtend())
if (attrKindId == 0) {
throw Error("Unable to find '$attributeName' attribute kind id")
}
return nounwindAttrKindId
return attrKindId
}
fun setFunctionNoUnwind(function: LLVMValueRef) {