Attempt of listOf() support. (#92)
This commit is contained in:
+1
-1
@@ -24,11 +24,11 @@ internal class Context(val irModule: IrModuleFragment, val runtime: Runtime, val
|
|||||||
|
|
||||||
val allocInstanceFunction = importRtFunction("AllocInstance")
|
val allocInstanceFunction = importRtFunction("AllocInstance")
|
||||||
val allocArrayFunction = importRtFunction("AllocArrayInstance")
|
val allocArrayFunction = importRtFunction("AllocArrayInstance")
|
||||||
|
val setArrayFunction = importRtFunction("Kotlin_Array_set")
|
||||||
val lookupFieldOffset = importRtFunction("LookupFieldOffset")
|
val lookupFieldOffset = importRtFunction("LookupFieldOffset")
|
||||||
val lookupOpenMethodFunction = importRtFunction("LookupOpenMethod")
|
val lookupOpenMethodFunction = importRtFunction("LookupOpenMethod")
|
||||||
val isInstanceFunction = importRtFunction("IsInstance")
|
val isInstanceFunction = importRtFunction("IsInstance")
|
||||||
val checkInstanceFunction = importRtFunction("CheckInstance")
|
val checkInstanceFunction = importRtFunction("CheckInstance")
|
||||||
val throwExceptionFunction = importRtFunction("ThrowException")
|
val throwExceptionFunction = importRtFunction("ThrowException")
|
||||||
|
|
||||||
val usedFunctions = mutableListOf<LLVMValueRef>()
|
val usedFunctions = mutableListOf<LLVMValueRef>()
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-1
@@ -596,6 +596,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
|||||||
is IrStringConcatenation -> return evaluateStringConcatenation(tmpVariableName, value)
|
is IrStringConcatenation -> return evaluateStringConcatenation(tmpVariableName, value)
|
||||||
is IrBlockBody -> return evaluateBlock (tmpVariableName, value as IrStatementContainer)
|
is IrBlockBody -> return evaluateBlock (tmpVariableName, value as IrStatementContainer)
|
||||||
is IrWhileLoop -> return evaluateWhileLoop ( value)
|
is IrWhileLoop -> return evaluateWhileLoop ( value)
|
||||||
|
is IrVararg -> return evaluateVararg (tmpVariableName, value)
|
||||||
null -> return null
|
null -> return null
|
||||||
else -> {
|
else -> {
|
||||||
TODO("${ir2string(value)}")
|
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? {
|
private fun evaluateStringConcatenation(tmpVariableName: String, value: IrStringConcatenation): LLVMValueRef? {
|
||||||
@@ -693,7 +708,6 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
|||||||
return concatResult
|
return concatResult
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------------------------------//
|
//-------------------------------------------------------------------------//
|
||||||
|
|
||||||
private fun evaluateThrow(tmpVariableName: String, expression: IrThrow): LLVMValueRef? {
|
private fun evaluateThrow(tmpVariableName: String, expression: IrThrow): LLVMValueRef? {
|
||||||
|
|||||||
+5
@@ -96,6 +96,10 @@ internal val int8TypePtr = pointerType(int8Type)
|
|||||||
|
|
||||||
internal val voidType = LLVMVoidType()
|
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
|
internal val ContextUtils.kTypeInfo: LLVMTypeRef
|
||||||
get() = LLVMGetTypeByName(context.llvmModule, "struct.TypeInfo")!!
|
get() = LLVMGetTypeByName(context.llvmModule, "struct.TypeInfo")!!
|
||||||
internal val ContextUtils.kObjHeader: LLVMTypeRef
|
internal val ContextUtils.kObjHeader: LLVMTypeRef
|
||||||
@@ -113,6 +117,7 @@ internal val ContextUtils.kTypeInfoPtr: LLVMTypeRef
|
|||||||
internal val kInt1 = LLVMInt1Type()
|
internal val kInt1 = LLVMInt1Type()
|
||||||
internal val kInt8Ptr = pointerType(LLVMInt8Type())
|
internal val kInt8Ptr = pointerType(LLVMInt8Type())
|
||||||
internal val kInt8PtrPtr = pointerType(kInt8Ptr)
|
internal val kInt8PtrPtr = pointerType(kInt8Ptr)
|
||||||
|
internal val kImmInt32One = Int32(1).getLlvmValue()
|
||||||
internal val ContextUtils.kNullObjHeaderPtr: LLVMValueRef
|
internal val ContextUtils.kNullObjHeaderPtr: LLVMValueRef
|
||||||
get() = LLVMConstNull(this.kObjHeaderPtr)!!
|
get() = LLVMConstNull(this.kObjHeaderPtr)!!
|
||||||
internal val ContextUtils.kNullArrayHeaderPtr: LLVMValueRef
|
internal val ContextUtils.kNullArrayHeaderPtr: LLVMValueRef
|
||||||
|
|||||||
@@ -453,11 +453,18 @@ task array_list1(type: RunKonanTest) {
|
|||||||
source = "runtime/collections/array_list1.kt"
|
source = "runtime/collections/array_list1.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task listof0(type: RunKonanTest) {
|
||||||
|
goldValue = "abc\n"
|
||||||
|
source = "runtime/collections/listof0.kt"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
task moderately_large_array(type: RunKonanTest) {
|
task moderately_large_array(type: RunKonanTest) {
|
||||||
goldValue = "0\n"
|
goldValue = "0\n"
|
||||||
source = "runtime/collections/moderately_large_array.kt"
|
source = "runtime/collections/moderately_large_array.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
task string_builder0(type: RunKonanTest) {
|
task string_builder0(type: RunKonanTest) {
|
||||||
goldValue = "OK\n"
|
goldValue = "OK\n"
|
||||||
source = "runtime/text/string_builder0.kt"
|
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()
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ package kotlin.collections
|
|||||||
* either throwing exception or returning some kind of implementation-specific default value.
|
* either throwing exception or returning some kind of implementation-specific default value.
|
||||||
*/
|
*/
|
||||||
fun <E> arrayOfLateInitElements(size: Int): Array<E> {
|
fun <E> arrayOfLateInitElements(size: Int): Array<E> {
|
||||||
// TODO: maybe use an empoty array.
|
// TODO: maybe use an empty array, fix array creation.
|
||||||
// return (if (size == 0) emptyArray else Array<Any>(size)) as Array<E>
|
// return (if (size == 0) emptyArray else Array<Any>(size)) as Array<E>
|
||||||
return Array<Any>(size) as Array<E>
|
return Array<Any>(size) as Array<E>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,3 +22,21 @@ public interface MutableIterable<out T> : Iterable<T> {
|
|||||||
*/
|
*/
|
||||||
override fun iterator(): MutableIterator<T>
|
override fun iterator(): MutableIterator<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun <T> arrayListOf(vararg args: T): MutableList<T> {
|
||||||
|
// TODO: fix me!
|
||||||
|
val result = ArrayList<Any>(args.size) as ArrayList<T>
|
||||||
|
for (arg in args) {
|
||||||
|
result.add(arg)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* FIXME: Suggested code from @olonho is following
|
||||||
|
*
|
||||||
|
* public fun <T> listOf(element: T): List<T> = arrayListOf(element)
|
||||||
|
*
|
||||||
|
* but in Big Kotlin this function is following: (see libraries/stdlib/src/kotlin/collections/Collections.kt)
|
||||||
|
* public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user