[codegen][clean up] warnings, inspections and absolete comments

- useless_is_checked
- unreachable_code
- a lot of unneeded string templates
- removed extra curly backets in string templates.
This commit is contained in:
Vasily Levchenko
2017-06-30 10:50:41 +03:00
committed by vvlevchenko
parent 33993236de
commit 56c1f2b07c
2 changed files with 35 additions and 45 deletions
@@ -444,8 +444,8 @@ internal class ContextFinderVisitor(val needle: IrElement) : IrElementVisitorVoi
stack.removeAt(stack.size - 1) stack.removeAt(stack.size - 1)
} }
override fun visitModuleFragment(module: IrModuleFragment) { override fun visitModuleFragment(declaration: IrModuleFragment) {
module.acceptChildrenVoid(this) declaration.acceptChildrenVoid(this)
} }
} }
@@ -51,8 +51,6 @@ import org.jetbrains.kotlin.types.typeUtil.isUnit
internal fun emitLLVM(context: Context) { internal fun emitLLVM(context: Context) {
val config = context.config.configuration
val irModule = context.irModule!! val irModule = context.irModule!!
// Note that we don't set module target explicitly. // Note that we don't set module target explicitly.
@@ -90,7 +88,7 @@ internal fun produceOutput(context: Context) {
when (config.get(KonanConfigKeys.PRODUCE)) { when (config.get(KonanConfigKeys.PRODUCE)) {
CompilerOutputKind.PROGRAM -> { CompilerOutputKind.PROGRAM -> {
val program = context.config.outputName val program = context.config.outputName
val output = "${program}.kt.bc" val output = "$program.kt.bc"
context.bitcodeFileName = output context.bitcodeFileName = output
PhaseManager(context).phase(KonanPhase.BITCODE_LINKER) { PhaseManager(context).phase(KonanPhase.BITCODE_LINKER) {
@@ -140,9 +138,9 @@ internal fun verifyModule(llvmModule: LLVMModuleRef, current: String = "") {
if (LLVMVerifyModule( if (LLVMVerifyModule(
llvmModule, LLVMVerifierFailureAction.LLVMPrintMessageAction, errorRef.ptr) == 1) { llvmModule, LLVMVerifierFailureAction.LLVMPrintMessageAction, errorRef.ptr) == 1) {
if (current.isNotEmpty()) if (current.isNotEmpty())
println("Error in ${current}") println("Error in $current")
LLVMDumpModule(llvmModule) LLVMDumpModule(llvmModule)
throw Error("Invalid module"); throw Error("Invalid module")
} }
} }
} }
@@ -309,12 +307,12 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
} }
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
override fun visitModuleFragment(module: IrModuleFragment) { override fun visitModuleFragment(declaration: IrModuleFragment) {
context.log{"visitModule : ${ir2string(module)}"} context.log{"visitModule : ${ir2string(declaration)}"}
// computeLifetimes(module, this.codegen, resultLifetimes) // computeLifetimes(module, this.codegen, resultLifetimes)
module.acceptChildrenVoid(this) declaration.acceptChildrenVoid(this)
appendLlvmUsed(context.llvm.usedFunctions) appendLlvmUsed(context.llvm.usedFunctions)
appendStaticInitializers(context.llvm.staticInitializers) appendStaticInitializers(context.llvm.staticInitializers)
appendEntryPointSelector(findMainEntryPoint(context)) appendEntryPointSelector(findMainEntryPoint(context))
@@ -447,14 +445,14 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
override fun visitConstructor(constructorDeclaration: IrConstructor) { override fun visitConstructor(declaration: IrConstructor) {
context.log{"visitConstructor : ${ir2string(constructorDeclaration)}"} context.log{"visitConstructor : ${ir2string(declaration)}"}
if (constructorDeclaration.descriptor.containingDeclaration.isIntrinsic) { if (declaration.descriptor.containingDeclaration.isIntrinsic) {
// Do not generate any ctors for intrinsic classes. // Do not generate any ctors for intrinsic classes.
return return
} }
val constructorDescriptor = constructorDeclaration.descriptor val constructorDescriptor = declaration.descriptor
val classDescriptor = constructorDescriptor.constructedClass val classDescriptor = constructorDescriptor.constructedClass
if (constructorDescriptor.isPrimary) { if (constructorDescriptor.isPrimary) {
if (DescriptorUtils.isObject(classDescriptor)) { if (DescriptorUtils.isObject(classDescriptor)) {
@@ -466,7 +464,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
} }
} }
visitFunction(constructorDeclaration) visitFunction(declaration)
} }
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
@@ -650,18 +648,18 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
override fun visitField(expression: IrField) { override fun visitField(declaration: IrField) {
context.log{"visitField : ${ir2string(expression)}"} context.log{"visitField : ${ir2string(declaration)}"}
debugFieldDeclaration(expression) debugFieldDeclaration(declaration)
val descriptor = expression.descriptor val descriptor = declaration.descriptor
if (descriptor.containingDeclaration is PackageFragmentDescriptor) { if (descriptor.containingDeclaration is PackageFragmentDescriptor) {
val type = codegen.getLLVMType(descriptor.type) val type = codegen.getLLVMType(descriptor.type)
val globalProperty = context.llvmDeclarations.forStaticField(descriptor).storage val globalProperty = context.llvmDeclarations.forStaticField(descriptor).storage
if (expression.initializer!!.expression is IrConst<*>) { if (declaration.initializer!!.expression is IrConst<*>) {
LLVMSetInitializer(globalProperty, evaluateExpression(expression.initializer!!.expression)) LLVMSetInitializer(globalProperty, evaluateExpression(declaration.initializer!!.expression))
} else { } else {
LLVMSetInitializer(globalProperty, LLVMConstNull(type)) LLVMSetInitializer(globalProperty, LLVMConstNull(type))
context.llvm.fileInitializers.add(expression) context.llvm.fileInitializers.add(declaration)
} }
LLVMSetLinkage(globalProperty, LLVMLinkage.LLVMInternalLinkage) LLVMSetLinkage(globalProperty, LLVMLinkage.LLVMInternalLinkage)
@@ -703,7 +701,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
return evaluateSuspendableExpression (value) return evaluateSuspendableExpression (value)
is IrSuspensionPoint -> return evaluateSuspensionPoint (value) is IrSuspensionPoint -> return evaluateSuspensionPoint (value)
else -> { else -> {
TODO("${ir2string(value)}") TODO(ir2string(value))
} }
} }
} }
@@ -725,7 +723,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
return codegen.theUnitInstanceRef.llvm return codegen.theUnitInstanceRef.llvm
} }
var objectPtr = getObjectInstanceStorage(value.descriptor) val objectPtr = getObjectInstanceStorage(value.descriptor)
val bbCurrent = codegen.currentBlock val bbCurrent = codegen.currentBlock
val bbInit = codegen.basicBlock("label_init") val bbInit = codegen.basicBlock("label_init")
val bbExit = codegen.basicBlock("label_continue") val bbExit = codegen.basicBlock("label_continue")
@@ -921,8 +919,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
} }
// The call inside [CatchingScope] must be configured to dispatch exception to the scope's handler. // The call inside [CatchingScope] must be configured to dispatch exception to the scope's handler.
override fun genCall(function: LLVMValueRef, args: List<LLVMValueRef>, lifetime: Lifetime): LLVMValueRef { override fun genCall(function: LLVMValueRef, args: List<LLVMValueRef>, resultLifetime: Lifetime): LLVMValueRef {
val res = codegen.call(function, args, lifetime, this::landingpad) val res = codegen.call(function, args, resultLifetime, this::landingpad)
return res return res
} }
@@ -1246,11 +1244,11 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
private fun genInstanceOf(obj: LLVMValueRef, type: KotlinType): LLVMValueRef { private fun genInstanceOf(obj: LLVMValueRef, type: KotlinType): LLVMValueRef {
var dstDescriptor = TypeUtils.getClassDescriptor(type) // Get class descriptor for dst type. val dstDescriptor = TypeUtils.getClassDescriptor(type) ?: return kTrue // Get class descriptor for dst type.
// Reified parameters are not yet supported. // Reified parameters are not yet supported.
if (dstDescriptor == null) return kTrue // Workaround for reified parameters // Workaround for reified parameters
val dstTypeInfo = codegen.typeInfoValue(dstDescriptor!!) // Get TypeInfo for dst type. val dstTypeInfo = codegen.typeInfoValue(dstDescriptor) // Get TypeInfo for dst type.
val srcObjInfoPtr = codegen.bitcast(codegen.kObjHeaderPtr, obj) // Cast src to ObjInfoPtr. val srcObjInfoPtr = codegen.bitcast(codegen.kObjHeaderPtr, obj) // Cast src to ObjInfoPtr.
val args = listOf(srcObjInfoPtr, dstTypeInfo) // Create arg list. val args = listOf(srcObjInfoPtr, dstTypeInfo) // Create arg list.
@@ -1391,7 +1389,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
IrConstKind.Float -> return LLVMConstRealOfString(LLVMFloatType(), (value.value as Float).toString())!! IrConstKind.Float -> return LLVMConstRealOfString(LLVMFloatType(), (value.value as Float).toString())!!
IrConstKind.Double -> return LLVMConstRealOfString(LLVMDoubleType(), (value.value as Double).toString())!! IrConstKind.Double -> return LLVMConstRealOfString(LLVMDoubleType(), (value.value as Double).toString())!!
} }
TODO("${ir2string(value)}") TODO(ir2string(value))
} }
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
@@ -1510,7 +1508,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
private fun evaluateContainerExpression(value: IrContainerExpression): LLVMValueRef { private fun evaluateContainerExpression(value: IrContainerExpression): LLVMValueRef {
context.log{"evaluateContainerExpression : ${value.statements.forEach { ir2string(it) }}"} context.log{"evaluateContainerExpression : ${value.statements.forEach { ir2string(it) }}"}
val scope = if (value is IrContainerExpression && value.isTransparentScope) { val scope = if (value.isTransparentScope) {
null null
} else { } else {
VariableScope() VariableScope()
@@ -1602,7 +1600,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
} }
} }
} }
TODO(ir2string(value))
} }
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
@@ -1669,6 +1666,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
scope.offsetInBits += sizeInBits scope.offsetInBits += sizeInBits
val alignInBits = expression.descriptor.type.alignment(context) val alignInBits = expression.descriptor.type.alignment(context)
scope.offsetInBits = alignTo(scope.offsetInBits, alignInBits) scope.offsetInBits = alignTo(scope.offsetInBits, alignInBits)
@Suppress("UNCHECKED_CAST")
scope.members.add(DICreateMemberType( scope.members.add(DICreateMemberType(
refBuilder = context.debugInfo.builder, refBuilder = context.debugInfo.builder,
refScope = scope.scope as DIScopeOpaqueRef, refScope = scope.scope as DIScopeOpaqueRef,
@@ -1830,7 +1828,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
private fun evaluateFunctionCall(callee: IrCall, args: List<LLVMValueRef>, private fun evaluateFunctionCall(callee: IrCall, args: List<LLVMValueRef>,
resultLifetime: Lifetime): LLVMValueRef { resultLifetime: Lifetime): LLVMValueRef {
val descriptor = callee.descriptor as FunctionDescriptor val descriptor = callee.descriptor
val argsWithContinuationIfNeeded = if (descriptor.isSuspend) val argsWithContinuationIfNeeded = if (descriptor.isSuspend)
args + getContinuation() args + getContinuation()
@@ -1869,18 +1867,18 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
memScoped { memScoped {
val constructedClass = (callee.descriptor as ConstructorDescriptor).constructedClass val constructedClass = (callee.descriptor as ConstructorDescriptor).constructedClass
val thisValue = if (constructedClass.isArray) { val thisValue = if (constructedClass.isArray) {
assert(args.size >= 1 && args[0].type == int32Type) assert(args.isNotEmpty() && args[0].type == int32Type)
codegen.allocArray(codegen.typeInfoValue(constructedClass), args[0], codegen.allocArray(codegen.typeInfoValue(constructedClass), args[0],
resultLifetime(callee)) resultLifetime(callee))
} else if (constructedClass == context.builtIns.string) { } else if (constructedClass == context.builtIns.string) {
// TODO: consider returning the empty string literal instead. // TODO: consider returning the empty string literal instead.
assert(args.size == 0) assert(args.isEmpty())
codegen.allocArray(codegen.typeInfoValue(constructedClass), count = kImmZero, codegen.allocArray(codegen.typeInfoValue(constructedClass), count = kImmZero,
lifetime = resultLifetime(callee)) lifetime = resultLifetime(callee))
} else { } else {
codegen.allocInstance(codegen.typeInfoValue(constructedClass), resultLifetime(callee)) codegen.allocInstance(codegen.typeInfoValue(constructedClass), resultLifetime(callee))
} }
evaluateSimpleFunctionCall(callee.descriptor as FunctionDescriptor, evaluateSimpleFunctionCall(callee.descriptor,
listOf(thisValue) + args, Lifetime.IRRELEVANT /* constructor doesn't return anything */) listOf(thisValue) + args, Lifetime.IRRELEVANT /* constructor doesn't return anything */)
return thisValue return thisValue
} }
@@ -2093,7 +2091,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
if (args.isEmpty()) return if (args.isEmpty()) return
memScoped { memScoped {
val arrayLength = args.size
val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) } val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) }
val llvmUsedGlobal = val llvmUsedGlobal =
context.llvm.staticData.placeGlobalArray("llvm.used", int8TypePtr, argsCasted) context.llvm.staticData.placeGlobalArray("llvm.used", int8TypePtr, argsCasted)
@@ -2133,7 +2130,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
val entryPoint = codegen.llvmFunction(descriptor) val entryPoint = codegen.llvmFunction(descriptor)
val selectorName = "EntryPointSelector" val selectorName = "EntryPointSelector"
val entryPointType = getFunctionType(entryPoint)!! val entryPointType = getFunctionType(entryPoint)
val selector = entryPointSelector(entryPoint, entryPointType, selectorName) val selector = entryPointSelector(entryPoint, entryPointType, selectorName)
@@ -2175,13 +2172,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
} }
//-------------------------------------------------------------------------// //-------------------------------------------------------------------------//
/**
* This block is devoted to coming codegen refactoring:
* removing [Codegenerator.currentFunction] of [FunctionDesctiptor]
* and working with local variables with [FunctionScope] and other enhancements.
*/
// TODO: is described refactoring still coming?
fun CodeGenerator.basicBlock(name: String, code: () -> Unit) = basicBlock(name).apply { fun CodeGenerator.basicBlock(name: String, code: () -> Unit) = basicBlock(name).apply {
appendingTo(this) { appendingTo(this) {