diff --git a/backend.native/tests/Makefile b/backend.native/tests/Makefile new file mode 100644 index 00000000000..33df8c2c53f --- /dev/null +++ b/backend.native/tests/Makefile @@ -0,0 +1,43 @@ +TESTS := sum + +BIN_TESTS=$(foreach t, ${TESTS},${t}_test) + +VPATH=codegen/function + +LLC=llc-mp-3.8 +CC=clang-mp-3.8 +JAVA=java + +TOP=../.. +RUNTIME=${TOP}/runtime +BACKEND=${TOP}/backend.native +BACKEND_CLASSES=${TOP}/backend.native/build/classes +KOTLIN_DIST=${BACKEND}/kotlin-ir/dist +KOTLIN_NATIVE_CLASSPATH=${BACKEND_CLASSES}/bc_frontend:${BACKEND_CLASSES}/cli_bc:${BACKEND_CLASSES}/compiler:${KOTLIN_DIST}/kotlinc/lib/kotlin-reflect.jar:${KOTLIN_DIST}kotlinc/lib/kotlin-stdlib.jar:${KOTLIN_DIST}/kotlinc/lib/kotlin-compiler.jar:${KOTLIN_DIST}/kotlinc/lib/kotlin-runtime.jar:${TOP}/Interop/Runtime/build/classes/main + + +define PROTO +$(1)_test: main.c $1-test.c $1.S + $(CC) -o $$@ $$^ + +${1}_run:${1}_test + ./${1}_test +endef + +$(foreach test,${TESTS},$(eval $(call PROTO,$(test)))) + +all:${BIN_TESTS} + +run:$(foreach test,${TESTS},${test}_run) + +clean: + ${RM} *.o *.S *.BCkt ${BIN_TESTS} + + +%.S:%.BCkt + ${LLC} -o $@ $< + + +%.BCkt:%.kt + ${JAVA} -cp ${KOTLIN_NATIVE_CLASSPATH} -Djava.library.path=${BACKEND}/build/nativelibs org.jetbrains.kotlin.cli.bc.K2NativeKt -output $@ -runtime ${RUNTIME}/build/runtime.bc $< + diff --git a/backend.native/tests/codegen/function/sum-test.c b/backend.native/tests/codegen/function/sum-test.c new file mode 100644 index 00000000000..2658fe58131 --- /dev/null +++ b/backend.native/tests/codegen/function/sum-test.c @@ -0,0 +1,10 @@ +extern void *resolve_symbol(const char*); + +int +run_test() { + int (*sum)(int, int) = resolve_symbol("kfun:sum"); + + if (sum(2, 3) != 5) return 1; + + return 0; +} diff --git a/sum.kt b/backend.native/tests/codegen/function/sum.kt similarity index 100% rename from sum.kt rename to backend.native/tests/codegen/function/sum.kt diff --git a/sum_func.kt b/backend.native/tests/codegen/function/sum_func.kt similarity index 100% rename from sum_func.kt rename to backend.native/tests/codegen/function/sum_func.kt diff --git a/sum_silly.kt b/backend.native/tests/codegen/function/sum_silly.kt similarity index 100% rename from sum_silly.kt rename to backend.native/tests/codegen/function/sum_silly.kt diff --git a/backend.native/tests/main.c b/backend.native/tests/main.c new file mode 100644 index 00000000000..e6f0c27defd --- /dev/null +++ b/backend.native/tests/main.c @@ -0,0 +1,19 @@ +#include +#include +/** + * > 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(); + +void * resolve_symbol(char *name) { + /* here we can add here some magic to resolve symbols in kotlin native*/ + return dlsym(RTLD_SELF, name); +} + + +int +main() { + return run_test(); +}