Transfer C argv to Kotlin's args.

This commit is contained in:
Alexander Gorshenev
2016-11-30 20:30:32 +03:00
committed by alexander-gorshenev
parent e38ac775ad
commit 3f480eb846
9 changed files with 78 additions and 38 deletions
+14 -2
View File
@@ -12,10 +12,12 @@ abstract class KonanTest extends DefaultTask {
def runtimeProject = project.project(":runtime")
def llvmLlc = llvmTool("llc")
def runtimeBc = new File("${runtimeProject.buildDir.canonicalPath}/runtime.bc")
def launcherBc = new File("${runtimeProject.buildDir.canonicalPath}/launcher.bc")
def stdlibKtBc = new File("${runtimeProject.buildDir.canonicalPath}/stdlib.kt.bc")
def mainC = 'main.c'
String goldValue = null
String testData = null
List<String> arguments = null
public KonanTest(){
dependsOn([project.project(":runtime").tasks['build'],
@@ -59,6 +61,9 @@ abstract class KonanTest extends DefaultTask {
def out = null
project.exec {
commandLine "${exe.absolutePath}"
if (arguments != null) {
args arguments
}
if (testData != null) {
standardInput = new ByteArrayInputStream(testData.bytes)
}
@@ -131,8 +136,8 @@ class UnitKonanTest extends KonanTest {
class RunKonanTest extends KonanTest {
void compileTest(File sourceS, File runtimeS, File libraryPath, File exe) {
project.execClang {
commandLine "clang++", "-DRUN_TEST", runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath,
"-o", exe.absolutePath, linkDl(), linkM(), rdynamic(), '-xc', mainC
commandLine "clang++", launcherBc.absolutePath, runtimeS.absolutePath, stdlibKtBc.absolutePath, sourceS.absolutePath,
"-o", exe.absolutePath, linkDl(), linkM(), rdynamic(), '-xc'
}
}
}
@@ -599,3 +604,10 @@ task range0(type: RunKonanTest) {
goldValue = "123\nabcd\n"
source = "runtime/collections/range0.kt"
}
task args0(type: RunKonanTest) {
arguments = ["AAA", "BB", "C"]
goldValue = "AAA\nBB\nC\n"
source = "runtime/basic/args0.kt"
}
+1 -12
View File
@@ -4,10 +4,6 @@
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
/**
* > llc-mp-3.8 b.out -o b.S
* > /opt/local/libexec/llvm-3.8/bin/clang main.c b.S -o sum-test
*/
extern int run_test();
@@ -24,13 +20,6 @@ void * resolve_symbol(char *name) {
}
int
kotlinNativeMain() {
#ifdef RUN_TEST
void (*main)(void *) = resolve_symbol("kfun:main(Array<String>)");
main((void *)0);
return 0;
#else
main() {
exit(run_test());
#endif
}
@@ -0,0 +1,5 @@
fun main(args : Array<String>) {
for (s in args) {
println(s)
}
}
+8
View File
@@ -8,10 +8,18 @@ task build(type: CompileCppToBitcode) {
srcRoot file('src/main')
dependsOn ':common:compileHash'
dependsOn 'launcher'
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
linkerArgs project.file('../common/build/hash.bc').path
}
task launcher(type: CompileCppToBitcode) {
name 'launcher'
srcRoot file('src/launcher')
compilerArgs '-I' + project.file('../common/src/hash/headers') // TODO: copy-pasted from 'common'
compilerArgs '-I' + project.file('src/main/cpp')
}
task clean << {
delete buildDir
}
+37
View File
@@ -0,0 +1,37 @@
#include <string.h>
#include "Memory.h"
#include "Natives.h"
#include "Types.h"
extern "C" ArrayHeader* konanArrayInit(ArrayHeader*, int)
asm("_kfun:kotlin.Array.<init>(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);
for (int i = 0; i < argc-1; i++) {
Kotlin_Array_set(args, i, (ArrayHeader*) makeString( argv[i+1] ));
}
return args;
}
extern "C" void konanMain(void*) asm("_kfun:main(Array<String>)");
int main(int argc, char** argv) {
InitMemory();
ArrayHeader* args = setupArgs(argc, argv);
konanMain(args);
// Yes, we have to follow Java convention and return zero.
return 0;
}
+11
View File
@@ -180,4 +180,15 @@ 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"
+2
View File
@@ -39,6 +39,8 @@ 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);
-15
View File
@@ -7,21 +7,6 @@
#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) {
-9
View File
@@ -1,9 +0,0 @@
#include "Memory.h"
extern "C" void kotlinNativeMain();
int main() {
InitMemory();
kotlinNativeMain();
return 0;
}