From 2c89fee837c4684a1337f548e9f8f95a142cc081 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Thu, 1 Dec 2016 15:58:27 +0300 Subject: [PATCH] Simplified launcher to utilize some ready made functionality. --- backend.native/tests/build.gradle | 4 +++- runtime/src/launcher/cpp/launcher.cpp | 11 ++--------- runtime/src/main/cpp/Memory.cpp | 11 +++++++++++ runtime/src/main/cpp/Memory.h | 14 ++------------ runtime/src/main/cpp/Natives.cpp | 11 ----------- runtime/src/main/cpp/Natives.h | 14 ++++++++++++-- runtime/src/main/cpp/ToString.cpp | 15 +++++++++++++++ runtime/src/main/cpp/Types.h | 1 + 8 files changed, 46 insertions(+), 35 deletions(-) diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 2f28613af01..2bd253bcb25 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -23,7 +23,9 @@ abstract class KonanTest extends DefaultTask { public KonanTest(){ dependsOn([project.project(":runtime").tasks['build'], project.parent.tasks['build'], - project.project(":backend.native").tasks['stdlib']]) + project.project(":backend.native").tasks['stdlib'], + project.project(":backend.native").tasks['start'] + ]) } diff --git a/runtime/src/launcher/cpp/launcher.cpp b/runtime/src/launcher/cpp/launcher.cpp index ac71554b308..2eca9e7d20b 100644 --- a/runtime/src/launcher/cpp/launcher.cpp +++ b/runtime/src/launcher/cpp/launcher.cpp @@ -3,20 +3,13 @@ #include "Natives.h" #include "Types.h" -extern "C" ArrayHeader* konanArrayInit(ArrayHeader*, int) - asm("_kfun:kotlin.Array.(Int)"); - -extern "C" const TypeInfo konanArrayType - asm("_ktype:kotlin.Array"); - ArrayHeader* setupArgs(int argc, char** argv) { // The count is one less, because we skip argv[0] which is the binary name. - ArrayHeader* array = AllocArrayInstance(&konanArrayType, SCOPE_GLOBAL, argc-1); - ArrayHeader* args = konanArrayInit(array, argc-1); + ArrayHeader* args = AllocArrayInstance(theArrayTypeInfo, SCOPE_GLOBAL, argc-1); for (int i = 0; i < argc-1; i++) { - Kotlin_Array_set(args, i, (ArrayHeader*) makeString( argv[i+1] )); + Kotlin_Array_set(args, i, AllocStringInstance( argv[i+1] )); } return args; diff --git a/runtime/src/main/cpp/Memory.cpp b/runtime/src/main/cpp/Memory.cpp index f51d4f22b6b..b0caebf0b2c 100644 --- a/runtime/src/main/cpp/Memory.cpp +++ b/runtime/src/main/cpp/Memory.cpp @@ -1,8 +1,10 @@ +#include #include #include "Assert.h" #include "Exceptions.h" #include "Memory.h" +#include "Natives.h" void FreeObject(ContainerHeader* header) { header->ref_count_ = CONTAINER_TAG_INVALID; @@ -87,6 +89,15 @@ ArrayHeader* AllocArrayInstance( return ArrayContainer(type_info, elements).GetPlace(); } +ArrayHeader* AllocStringInstance(const char* cstring) { + uint32_t length = strlen(cstring); + ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace(); + memcpy( + ByteArrayAddressOfElementAt(result, 0), + cstring, + length); + return result; +} #ifdef __cplusplus } #endif diff --git a/runtime/src/main/cpp/Memory.h b/runtime/src/main/cpp/Memory.h index 21d85c44af2..c66054d0387 100644 --- a/runtime/src/main/cpp/Memory.h +++ b/runtime/src/main/cpp/Memory.h @@ -79,18 +79,6 @@ struct ArenaContainerHeader : public ContainerHeader { uint8_t* end_; }; -inline void* AddressOfElementAt(ArrayHeader* obj, int32_t index) { - // Instance size is negative. - return reinterpret_cast(obj + 1) - - obj->type_info()->instanceSize_ * index; -} - -inline const void* AddressOfElementAt(const ArrayHeader* obj, int32_t index) { - // Instance size is negative. - return reinterpret_cast(obj + 1) - - obj->type_info()->instanceSize_ * index; -} - inline uint32_t ArrayDataSizeBytes(const ArrayHeader* obj) { // Instance size is negative. return -obj->type_info()->instanceSize_ * obj->count_; @@ -266,6 +254,8 @@ void InitMemory(); ObjHeader* AllocInstance(const TypeInfo* type_info, PlacementHint hint); ArrayHeader* AllocArrayInstance( const TypeInfo* type_info, PlacementHint hint, uint32_t elements); +ArrayHeader* AllocStringInstance(const char* cstring); + #ifdef __cplusplus } #endif diff --git a/runtime/src/main/cpp/Natives.cpp b/runtime/src/main/cpp/Natives.cpp index 7bd969785d3..baae7e3ba37 100644 --- a/runtime/src/main/cpp/Natives.cpp +++ b/runtime/src/main/cpp/Natives.cpp @@ -180,15 +180,4 @@ KString Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex) return result; } -KString makeString(const char* cstring) { - uint32_t length = strlen(cstring); - ArrayHeader* result = ArrayContainer( - theStringTypeInfo, length).GetPlace(); - memcpy( - ByteArrayAddressOfElementAt(result, 0), - cstring, - length); - return result; -} - } // extern "C" diff --git a/runtime/src/main/cpp/Natives.h b/runtime/src/main/cpp/Natives.h index aa3b7e7f2eb..c1b9dc9b190 100644 --- a/runtime/src/main/cpp/Natives.h +++ b/runtime/src/main/cpp/Natives.h @@ -3,6 +3,18 @@ #include "Types.h" +inline void* AddressOfElementAt(ArrayHeader* obj, int32_t index) { + // Instance size is negative. + return reinterpret_cast(obj + 1) - + obj->type_info()->instanceSize_ * index; +} + +inline const void* AddressOfElementAt(const ArrayHeader* obj, int32_t index) { + // Instance size is negative. + return reinterpret_cast(obj + 1) - + obj->type_info()->instanceSize_ * index; +} + // Optimized versions not accessing type info. inline KByte* ByteArrayAddressOfElementAt(ArrayHeader* obj, KInt index) { return reinterpret_cast(obj + 1) + index; @@ -39,8 +51,6 @@ extern "C" { KString TheEmptyString(); -KString makeString(const char* cstring); - // Any.kt KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other); KInt Kotlin_Any_hashCode(KConstRef thiz); diff --git a/runtime/src/main/cpp/ToString.cpp b/runtime/src/main/cpp/ToString.cpp index 806c89c7903..d73279e2878 100644 --- a/runtime/src/main/cpp/ToString.cpp +++ b/runtime/src/main/cpp/ToString.cpp @@ -7,6 +7,21 @@ #include "Natives.h" #include "Types.h" +namespace { + +KString makeString(const char* cstring) { + uint32_t length = strlen(cstring); + ArrayHeader* result = ArrayContainer( + theStringTypeInfo, length).GetPlace(); + memcpy( + ByteArrayAddressOfElementAt(result, 0), + cstring, + length); + return result; +} + +} // namespace + extern "C" { KString Kotlin_Byte_toString(KByte value) { diff --git a/runtime/src/main/cpp/Types.h b/runtime/src/main/cpp/Types.h index c39f67e85b2..dde452d8707 100644 --- a/runtime/src/main/cpp/Types.h +++ b/runtime/src/main/cpp/Types.h @@ -24,6 +24,7 @@ extern "C" { extern const TypeInfo* theAnyTypeInfo; extern const TypeInfo* theCloneableTypeInfo; +extern const TypeInfo* theArrayTypeInfo; extern const TypeInfo* theByteArrayTypeInfo; extern const TypeInfo* theCharArrayTypeInfo; extern const TypeInfo* theShortArrayTypeInfo;