Attempt of listOf() support. (#92)

This commit is contained in:
Nikolay Igotti
2016-12-01 16:09:26 +03:00
committed by GitHub
parent 85aca55ee5
commit f9167ea91e
7 changed files with 52 additions and 3 deletions
@@ -24,11 +24,11 @@ internal class Context(val irModule: IrModuleFragment, val runtime: Runtime, val
val allocInstanceFunction = importRtFunction("AllocInstance")
val allocArrayFunction = importRtFunction("AllocArrayInstance")
val setArrayFunction = importRtFunction("Kotlin_Array_set")
val lookupFieldOffset = importRtFunction("LookupFieldOffset")
val lookupOpenMethodFunction = importRtFunction("LookupOpenMethod")
val isInstanceFunction = importRtFunction("IsInstance")
val checkInstanceFunction = importRtFunction("CheckInstance")
val throwExceptionFunction = importRtFunction("ThrowException")
val usedFunctions = mutableListOf<LLVMValueRef>()
}
@@ -596,6 +596,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
is IrStringConcatenation -> return evaluateStringConcatenation(tmpVariableName, value)
is IrBlockBody -> return evaluateBlock (tmpVariableName, value as IrStatementContainer)
is IrWhileLoop -> return evaluateWhileLoop ( value)
is IrVararg -> return evaluateVararg (tmpVariableName, value)
null -> return null
else -> {
TODO("${ir2string(value)}")
@@ -678,6 +679,20 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
}
}
//-------------------------------------------------------------------------//
private fun evaluateVararg(tmpVariableName: String, value: IrVararg): LLVMValueRef? {
val arrayCreationArgs = listOf(codegen.kTheArrayTypeInfo!!, kImmInt32One!!, Int32(value.elements.size).getLlvmValue()!!)
val array = currentCodeContext.genCall(context.allocArrayFunction, arrayCreationArgs, tmpVariableName)
value.elements.forEachIndexed { i, it ->
val elementValueRaw = evaluateExpression(codegen.newVar(), it)
currentCodeContext.genCall(context.setArrayFunction, listOf(array!!, Int32(i).getLlvmValue()!!, elementValueRaw!!), "")
return@forEachIndexed
}
return array
}
//-------------------------------------------------------------------------//
private fun evaluateStringConcatenation(tmpVariableName: String, value: IrStringConcatenation): LLVMValueRef? {
@@ -693,7 +708,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
return concatResult
}
//-------------------------------------------------------------------------//
private fun evaluateThrow(tmpVariableName: String, expression: IrThrow): LLVMValueRef? {
@@ -96,6 +96,10 @@ internal val int8TypePtr = pointerType(int8Type)
internal val voidType = LLVMVoidType()
internal val ContextUtils.kTheAnyTypeInfo: LLVMValueRef?
get() = KonanPlatform.builtIns.any.llvmTypeInfoPtr.getLlvmValue()
internal val ContextUtils.kTheArrayTypeInfo: LLVMValueRef?
get() = KonanPlatform.builtIns.array.llvmTypeInfoPtr.getLlvmValue()
internal val ContextUtils.kTypeInfo: LLVMTypeRef
get() = LLVMGetTypeByName(context.llvmModule, "struct.TypeInfo")!!
internal val ContextUtils.kObjHeader: LLVMTypeRef
@@ -113,6 +117,7 @@ internal val ContextUtils.kTypeInfoPtr: LLVMTypeRef
internal val kInt1 = LLVMInt1Type()
internal val kInt8Ptr = pointerType(LLVMInt8Type())
internal val kInt8PtrPtr = pointerType(kInt8Ptr)
internal val kImmInt32One = Int32(1).getLlvmValue()
internal val ContextUtils.kNullObjHeaderPtr: LLVMValueRef
get() = LLVMConstNull(this.kObjHeaderPtr)!!
internal val ContextUtils.kNullArrayHeaderPtr: LLVMValueRef
+7
View File
@@ -453,11 +453,18 @@ task array_list1(type: RunKonanTest) {
source = "runtime/collections/array_list1.kt"
}
task listof0(type: RunKonanTest) {
goldValue = "abc\n"
source = "runtime/collections/listof0.kt"
}
task moderately_large_array(type: RunKonanTest) {
goldValue = "0\n"
source = "runtime/collections/moderately_large_array.kt"
}
task string_builder0(type: RunKonanTest) {
goldValue = "OK\n"
source = "runtime/text/string_builder0.kt"
@@ -0,0 +1,5 @@
fun main(args : Array<String>) {
val list = arrayListOf("a", "b", "c")
for (element in list) print(element)
println()
}